Why am I getting an error compiling the Bazel tutorial?

100 Views Asked by At

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?

2

There are 2 best solutions below

2
Vertexwahn On

When you use rules_cc (i.e. cc_binary, cc_library) and do not specify a toolchain/platform Bazel tries to autodetect a C++ toolchain that is installed on your local system - e.g. GCC 11 on Ubuntu 22.04, Visual C++ on Windows, Apple Clang on macOS. It seems that Bazel was not able to "autodetect" your local C++ toolchain. Maybe you do not have a C++ compiler installed on your system or it is installed at a strange location? Have a look at those instructions here: https://tutorialforlinux.com/how-to-install-bazel-on-mint-gnulinux-distro/. Or maybe the Bazel autodetection on Mint is not good enough. Then you should try to modify the cc_configure.bzl file.

Another option (but more complicated) would be to depend on a hermetic toolchain - that means you depend on a toolchain that is downloaded/fetched by Bazel from an external source - and is not dependent on something that is installed on your local system. Here you have different options:

For a first experience maybe also Docker can of help here. You can run Bazel inside a docker as described here. I used Bazel with Ubuntu 18, 20, 22, Windows 10/11, macOS 10/11/12/13/14 and the Hello World example was always working. Maybe you switch to one of those platforms for your first experiments.

0
Peter S On

I noticed that the contents of BUILD did not match the text on the Tutorial page so I kept checking out older versions of the repo. The tutorial stops working for me sometime between Sept 2023 and Jan 2024.

The way I got it to work was to do this -

git checkout ce5d8a6d5dcbbf9c875231a715f84e2862b5e137

Now I have an empty WORKSPACE and the first example will build using the instructions in the tutorial.