You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

全新安装Bazel后无法构建gRPC 1.48.0问题求助

问题描述

我是gRPC和Bazel的新手,想尝试用C++版本替换逐渐过时的Thrift。我的环境信息如下:

$ gcc --version
gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ bazel --version
INFO: Running bazel wrapper (see //tools/bazel for details), bazel version 4.2.1 will be used instead of system-wide bazel installation.
bazel 4.2.1

我参考了官方文档,但运行bazel build :all时出现自动C++工具链相关错误,已安装文档中提到的所有apt包,却不清楚为何系统无法识别gcc,错误日志如下:

bazel build :all
INFO: Running bazel wrapper (see //tools/bazel for details), bazel version 4.2.1 will be used instead of system-wide bazel installation.
DEBUG: /home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_toolchains/rules/rbe_repo/version_check.bzl:68:14: 
Current running Bazel is ahead of bazel-toolchains repo. Please update your pin to bazel-toolchains repo in your WORKSPACE file.
DEBUG: /home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_toolchains/rules/rbe_repo/checked_in.bzl:125:14: rbe_default not using checked in configs; Bazel version 4.2.1 was picked/selected but no checked in config was found in map {"0.20.0": ["8.0.0"], "0.21.0": ["8.0.0"], "0.22.0": ["8.0.0", "9.0.0"], "0.23.0": ["8.0.0", "9.0.0"], "0.23.1": ["8.0.0", "9.0.0"], "0.23.2": ["9.0.0"], "0.24.0": ["9.0.0"], "0.24.1": ["9.0.0"], "0.25.0": ["9.0.0"], "0.25.1": ["9.0.0"], "0.25.2": ["9.0.0"], "0.26.0": ["9.0.0"], "0.26.1": ["9.0.0"], "0.27.0": ["9.0.0"], "0.27.1": ["9.0.0"], "0.28.0": ["9.0.0"], "0.28.1": ["9.0.0"], "0.29.0": ["9.0.0"], "0.29.1": ["9.0.0", "10.0.0"], "1.0.0": ["9.0.0", "10.0.0"], "1.0.1": ["10.0.0"], "1.1.0": ["10.0.0"], "1.2.0": ["10.0.0"], "1.2.1": ["10.0.0"], "2.0.0": ["10.0.0"], "2.1.0": ["10.0.0"], "2.1.1": ["10.0.0", "11.0.0"], "2.2.0": ["11.0.0"], "3.0.0": ["11.0.0"], "3.1.0": ["11.0.0"], "3.2.0": ["11.0.0"], "3.3.0": ["11.0.0"], "3.3.1": ["11.0.0"], "3.4.1": ["11.0.0"], "3.5.0": ["11.0.0"], "3.5.1": ["11.0.0"], "3.6.0": ["11.0.0"], "3.7.0": ["11.0.0"], "3.7.1": ["11.0.0"], "3.7.2": ["11.0.0"], "4.0.0": ["11.0.0"]}
DEBUG: /home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_toolchains/rules/rbe_repo/version_check.bzl:68:14: 
Current running Bazel is ahead of bazel-toolchains repo. Please update your pin to bazel-toolchains repo in your WORKSPACE file.
DEBUG: /home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_toolchains/rules/rbe_repo/checked_in.bzl:125:14: rbe_msan not using checked in configs; Bazel version 4.2.1 was picked/selected but no checked in config was found in map {"0.20.0": ["8.0.0"], "0.21.0": ["8.0.0"], "0.22.0": ["8.0.0", "9.0.0"], "0.23.0": ["8.0.0", "9.0.0"], "0.23.1": ["8.0.0", "9.0.0"], "0.23.2": ["9.0.0"], "0.24.0": ["9.0.0"], "0.24.1": ["9.0.0"], "0.25.0": ["9.0.0"], "0.25.1": ["9.0.0"], "0.25.2": ["9.0.0"], "0.26.0": ["9.0.0"], "0.26.1": ["9.0.0"], "0.27.0": ["9.0.0"], "0.27.1": ["9.0.0"], "0.28.0": ["9.0.0"], "0.28.1": ["9.0.0"], "0.29.0": ["9.0.0"], "0.29.1": ["9.0.0", "10.0.0"], "1.0.0": ["9.0.0", "10.0.0"], "1.0.1": ["10.0.0"], "1.1.0": ["10.0.0"], "1.2.0": ["10.0.0"], "1.2.1": ["10.0.0"], "2.0.0": ["10.0.0"], "2.1.0": ["10.0.0"], "2.1.1": ["10.0.0", "11.0.0"], "2.2.0": ["11.0.0"], "3.0.0": ["11.0.0"], "3.1.0": ["11.0.0"], "3.2.0": ["11.0.0"], "3.3.0": ["11.0.0"], "3.3.1": ["11.0.0"], "3.4.1": ["11.0.0"], "3.5.0": ["11.0.0"], "3.5.1": ["11.0.0"], "3.6.0": ["11.0.0"], "3.7.0": ["11.0.0"], "3.7.1": ["11.0.0"], "3.7.2": ["11.0.0"], "4.0.0": ["11.0.0"]}
INFO: Repository local_config_cc instantiated at:
  /DEFAULT.WORKSPACE.SUFFIX:368:13: in <toplevel>
  /home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/cc_configure.bzl:184:16: in cc_configure
Repository rule cc_autoconf defined at:
  /home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/cc_configure.bzl:145:30: in <toplevel>
ERROR: An error occurred during the fetch of repository 'local_config_cc':
   Traceback (most recent call last):
        File "/home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/cc_configure.bzl", line 127, column 33, in cc_autoconf_impl
                configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools)
        File "/home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 340, column 23, in configure_unix_toolchain
                cc = _find_generic(repository_ctx, "gcc", "CC", overriden_tools)
        File "/home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 310, column 32, in _find_generic
                auto_configure_fail(msg)
        File "/home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/lib_cc_configure.bzl", line 112, column 9, in auto_configure_fail
                fail("\n%sAuto-Configuration Error:%s %s\n" % (red, no_color, msg))
Error in fail: 
Auto-Configuration Error: Cannot find gcc or CC (clang); either correct your path or set the CC environment variable
ERROR: Error fetching repository: Traceback (most recent call last):
        File "/home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/cc_configure.bzl", line 127, column 33, in cc_autoconf_impl
                configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools)
        File "/home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 340, column 23, in configure_unix_toolchain
                cc = _find_generic(repository_ctx, "gcc", "CC", overriden_tools)
        File "/home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 310, column 32, in _find_generic
                auto_configure_fail(msg)
        File "/home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/cpp/lib_cc_configure.bzl", line 112, column 9, in auto_configure_fail
                fail("\n%sAuto-Configuration Error:%s %s\n" % (red, no_color, msg))
Error in fail: 
Auto-Configuration Error: Cannot find gcc or CC (clang); either correct your path or set the CC environment variable
INFO: Repository com_google_absl instantiated at:
  /home/emcp/Dev/git/grpc/grpc/WORKSPACE:5:10: in <toplevel>
  /home/emcp/Dev/git/grpc/grpc/bazel/grpc_deps.bzl:295:21: in grpc_deps
Repository rule http_archive defined at:
  /home/emcp/.cache/bazel/_bazel_emcp/281ba5a21da7173aa4ab26dbe66e8d65/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
ERROR: /home/emcp/Dev/git/grpc/grpc/BUILD:1489:16: //:activity depends on @local_config_cc//:cc-compiler-k8 in repository @local_config_cc which failed to fetch. no such package '@local_config_cc//': 
Auto-Configuration Error: Cannot find gcc or CC (clang); either correct your path or set the CC environment variable
ERROR: Analysis of target '//:activity' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.517s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)
解决方案

1. 显式指定CC环境变量

Bazel可能无法自动识别gcc路径,直接在build命令前绑定gcc的绝对路径:

# 先获取gcc绝对路径
which gcc
# 替换路径后执行
CC=/usr/bin/gcc bazel build :all

2. 更新bazel-toolchains依赖版本

日志显示当前Bazel 4.2.1超出了bazel-toolchains仓库的支持范围,需更新WORKSPACE中的配置:

  • 打开grpc仓库根目录下的WORKSPACE文件
  • 找到bazel_toolchains的http_archive配置块,将版本标签更新为兼容Bazel 4.2.1的版本(比如"4.2.0"或更高匹配版本)
  • 保存后重新执行build命令

3. 清理Bazel缓存后重试

旧缓存可能导致工具链配置异常,清理后重新触发自动配置:

bazel clean --expunge
CC=/usr/bin/gcc bazel build :all

4. 验证gcc完整安装

确保gcc及相关编译依赖已完全安装:

sudo apt-get install build-essential gcc g++

内容的提问来源于stack exchange,提问作者Erik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 22:07:47