编译Bazel C++教程时出现错误,请求排查原因
Bazel 7.0.2 在 Mint 21 编译C++教程失败问题
问题背景
在Mint 21 Linux系统上使用Bazel 7.0.2尝试官方C++教程,本地已安装gcc,但Bazel仍自动下载了gcc工具链,编译hello world时报错,提示找不到下载的工具链文件。
报错详情
执行编译命令:
bazel build //main:hello-world --verbose_failures
关键错误信息:
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
完整编译日志:
~/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位于
/usr/bin,已在系统PATH中 - Bazel存储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:$
解决方案
1. 强制使用本地gcc工具链
跳过Bazel自动下载的工具链,直接调用系统自带gcc:
- 在项目根目录创建
.bazelrc文件,添加:
build --cxxopt=-std=c++17 build --host_cxxopt=-std=c++17 build --toolchain=//tools/cpp:gcc
- 或者直接在编译命令中指定:
bazel build //main:hello-world --verbose_failures --toolchain=//tools/cpp:gcc
2. 清理Bazel缓存后重建
缓存损坏可能导致工具链文件异常,清理后重新构建:
bazel clean --expunge bazel build //main:hello-world
3. 关闭沙箱编译
沙箱限制可能导致无法访问/tmp中的工具链文件,尝试本地策略编译:
bazel build //main:hello-world --verbose_failures --spawn_strategy=local
4. 补全系统依赖
确保gcc相关依赖完整,安装必要包:
sudo apt install build-essential gcc-multilib g++-multilib
内容的提问来源于stack exchange,提问作者Peter S
相关产品推荐
相关产品推荐

