4

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?

1
  • I have run into the exact same problem; no gcc in /tmp/external/... Running: Bazelisk version: v1.19.0 Build label: 7.1.1 Commented Apr 9, 2024 at 19:05

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

2 Comments

Well I managed to create a hello world from scratch by following an example on YouTube. That had an empty WORKSPACE file and a minimal BUILD file. In fact, the BUILD file only had in it what the tutorial page said their BUILD had in it. In reality, there's an extra line in the tutorial BUILD. There's also a lot of stuff in the WORKSPACE file. My compiler is a very standard /usr/bin/gcc so the simple example finds it and works. Seems that the tutorial is broken in some way to me
I posted my own answer - it'd be interesting to know if the tutorial still works for you. If I go back to a version of the git repo that is 6 months older, it all works fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.