c++bazelbazel-cpp

Why am I getting an error compiling the Bazel tutorial?


I know next to nothing about Bazel so I thought I'd try the tutorial on Mint 21 Linux. I installed bazel which is version 7.0.2.

I am following the instructions on the tutorial here - https://bazel.build/start/cpp

It has failed to compile hello world (stage 1). It took a while to get going as it downloaded the gcc toolset even though I have gcc installed. Then it failed. I don't know where to start!

I think it must be because it decided to download gcc and then couldn't find what it just downloaded? This is the output with --verbose_failures

~/work/bazel_examples/cpp-tutorial/stage1:main$ bazel build //main:hello-world --verbose_failures
INFO: Analyzed target //main:hello-world (69 packages loaded, 6447 targets configured).
ERROR: /home/peter/work/bazel_examples/cpp-tutorial/stage1/main/BUILD:3:10: Compiling main/hello-world.cc failed: (Exit 127): gcc failed: error executing CppCompile command (from target //main:hello-world) 
  (cd /home/peter/.cache/bazel/_bazel_peter/c1c98fada9611f3cc5dcbb685f6a36c0/sandbox/linux-sandbox/2/execroot/_main && \
  exec env - \
    BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 \
    PATH=/bin:/usr/bin:/usr/local/bin \
    PWD=/proc/self/cwd \
  external/gcc_toolchain_x86_64/bin/gcc -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF bazel-out/k8-fastbuild/bin/main/_objs/hello-world/hello-world.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/main/_objs/hello-world/hello-world.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools --sysroot external/sysroot_x86_64/ -no-canonical-prefixes -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' '-fdiagnostics-color=always' -nostdinc -nostdinc++ -Bexternal/gcc_toolchain_x86_64/bin -isystemexternal/sysroot_x86_64//include/c++/10.3.0 -isystemexternal/sysroot_x86_64//include/c++/10.3.0/x86_64-linux -isystemexternal/sysroot_x86_64//lib/gcc/x86_64-linux/10.3.0/include-fixed -isystemexternal/sysroot_x86_64//lib/gcc/x86_64-linux/10.3.0/include -isystemexternal/sysroot_x86_64//usr/include -c main/hello-world.cc -o bazel-out/k8-fastbuild/bin/main/_objs/hello-world/hello-world.pic.o)
# Configuration: 9a39f0e0b842e0d708a20b96ffa7524f9390f4975734ffa40bdebf80acf7e3d7
# Execution platform: @@local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
external/gcc_toolchain_x86_64/bin/gcc: line 45: /tmp/external/gcc_toolchain_x86_64_files/bin/x86_64-linux-gcc: No such file or directory
Target //main:hello-world failed to build
INFO: Elapsed time: 0.931s, Critical Path: 0.05s
INFO: 2 processes: 2 internal.
ERROR: Build did NOT complete successfully

gcc is on my PATH in /usr/bin. I found where it stashed the gcc_toolchain

find . -name gcc_toolchain_x86_64
./bazel/_bazel_peter/c1c98fada9611f3cc5dcbb685f6a36c0/execroot/_main/external/gcc_toolchain_x86_64
./bazel/_bazel_peter/c1c98fada9611f3cc5dcbb685f6a36c0/external/gcc_toolchain_x86_64
./bazel/_bazel_peter/c1c98fada9611f3cc5dcbb685f6a36c0/sandbox_stash/CppCompile/6/execroot/_main/external/gcc_toolchain_x86_64
./bazel/_bazel_peter/3af4bab8b660508f556d6c098b8f71d6/execroot/_main/external/gcc_toolchain_x86_64
./bazel/_bazel_peter/3af4bab8b660508f556d6c098b8f71d6/external/gcc_toolchain_x86_64
./bazel/_bazel_peter/3af4bab8b660508f556d6c098b8f71d6/sandbox_stash/CppCompile/13/execroot/_main/external/gcc_toolchain_x86_64
~/.cache:$ 

What silly mistake did I make?


Solution

  • The error when compiling cpp tutorial stage1 still on bazel 7.1.1 (Arch Linux).

    In my case this workaround worked:

    $ bazel build //main:hello-world --sandbox_add_mount_pair=/tmp

    Seems that the issue is not from bazel but the gcc toolchain.