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

Apple M1上Bazel构建C++17项目遇protoc符号缺失错误

Apple M1设备上Bazel构建C++17 Proto项目的dyld符号缺失问题

在Apple M1设备上使用Bazel构建包含Proto文件的C++17项目,已卸载系统protoc避免干扰,但仍出现dyld符号缺失错误。项目使用rules_proto,WORKSPACE相关配置如下:

http_archive(
    name = "rules_proto",
    sha256 = "afa0473881bd472a514254786f38527f8f8cf6dbf82bc24706c140a5bfeeb573",
    strip_prefix = "rules_proto-4.0.0-3.19.1",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
        "https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0-3.19.1.tar.gz",
    ],
)
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

http_archive(
    name = "com_github_grpc_grpc",
    urls = [
        "https://github.com/grpc/grpc/archive/refs/tags/v1.44.0.tar.gz",
    ],
    sha256 = "8c05641b9f91cbc92f51cc4a5b3a226788d7a63f20af4ca7aaca50d92cc94a0d",
    strip_prefix = "grpc-1.44.0",
)
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
grpc_extra_deps()

构建时出现如下错误:

INFO: Analyzed target //:lib (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /Users/user/work/proj/3rdparty/Project/cc/project/protos/BUILD:10:19: Action cc/project/protos/file.grpc.pb.h failed: (Aborted): protoc failed: error executing command 
  (cd /private/var/tmp/_bazel_user/dedfc382dcde4c27bfad3e37c75660b3/sandbox/darwin-sandbox/526/execroot/__main__ && \
  exec env - \
    PATH=/Users/user/Library/Caches/bazelisk/downloads/bazelbuild/bazel-5.4.0-darwin-arm64/bin:/Users/user/nvim-macos/bin/:/Users/user/perl5/bin/:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/bin:/Users/user/nvim-macos/bin:/Users/user/perl5/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/Caskroom/miniforge/base/bin:/opt/homebrew/Caskroom/miniforge/base/condabin:/Users/user/.nvm/versions/node/v17.6.0/bin:/Users/user/.cargo/bin:/opt/homebrew/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/user/nvim-macos/bin:/Users/user/perl5/bin:/Applications/kitty.app/Contents/MacOS:/Users/user/go/bin:/opt/homebrew/opt/go/libexec/bin:/Users/user/go/bin:/opt/homebrew/opt/go/libexec/bin:/opt/homebrew/opt/fzf/bin:/Users/user/go/bin:/opt/homebrew/opt/go/libexec/bin \
  bazel-out/host/bin/external/com_github_protocolbuffers_protobuf/protoc '--plugin=protoc-gen-PLUGIN=bazel-out/host/bin/external/com_github_grpc_grpc/src/compiler/grpc_cpp_plugin' '--PLUGIN_out=bazel-out/darwin_arm64-fastbuild/bin' '--proto_path=.' '--proto_path=bazel-out/darwin_arm64-fastbuild/bin' cc/project/protos/file.proto)
# Configuration: 0dea222a1412967d38294213555f40fafc2ad3d3044e96efc6da0592ce3c1478
# Execution platform: @local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
dyld[92815]: symbol not found in flat namespace '__ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev'
Target //:lib failed to build
ERROR: /Users/user/work/proj/3rdparty/Project/cc/project/protos/BUILD:10:19 Middleman _middlemen/_S_Scc_Sproject_Sprotos_Cfile-BazelCppSemantics_build_arch_darwin_arm64-fastbuild failed: (Aborted): protoc failed: error executing command 
  (cd /private/var/tmp/_bazel_user/dedfc382dcde4c27bfad3e37c75660b3/sandbox/darwin-sandbox/526/execroot/__main__ && \
  exec env - \
    PATH=/Users/user/Library/Caches/bazelisk/downloads/bazelbuild/bazel-5.4.0-darwin-arm64/bin:/Users/user/nvim-macos/bin/:/Users/user/perl5/bin/:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/bin:/Users/user/nvim-macos/bin:/Users/user/perl5/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/Caskroom/miniforge/base/bin:/opt/homebrew/Caskroom/miniforge/base/condabin:/Users/user/.nvm/versions/node/v17.6.0/bin:/Users/user/.cargo/bin:/opt/homebrew/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/user/nvim-macos/bin:/Users/user/perl5/bin:/Applications/kitty.app/Contents/MacOS:/Users/user/go/bin:/opt/homebrew/opt/go/libexec/bin:/Users/user/go/bin:/opt/homebrew/opt/go/libexec/bin:/opt/homebrew/opt/fzf/bin:/Users/user/go/bin:/opt/homebrew/opt/go/libexec/bin \
  bazel-out/host/bin/external/com_github_protocolbuffers_protobuf/protoc '--plugin=protoc-gen-PLUGIN=bazel-out/host/bin/external/com_github_grpc_grpc/src/compiler/grpc_cpp_plugin' '--PLUGIN_out=bazel-out/darwin_arm64-fastbuild/bin' '--proto_path=.' '--proto_path=bazel-out/darwin_arm64-fastbuild/bin' cc/project/protos/file.proto)
# Configuration: 0dea222a1412967d38294213555f40fafc2ad3d3044e96efc6da0592ce3c1478
# Execution platform: @local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
INFO: Elapsed time: 0.675s, Critical Path: 0.25s
INFO: 10 processes: 9 internal, 1 darwin-sandbox.
FAILED: Build did NOT complete successfully

同一项目在aarch64 Linux容器中使用相同命令可正常构建。


问题分析与解决方法

问题根源

错误中的__ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev是C11标准下std::string的析构函数符号,说明**protoc或grpc_cpp_plugin构建时使用的C标准/ABI与项目不一致**。M1的Darwin环境中,Bazel默认工具链的C++配置容易出现这类兼容性问题,而Linux环境下默认配置更统一,因此未出现错误。

解决步骤

  • 统一C++标准与ABI:在项目根目录的.bazelrc中添加以下配置,确保所有目标(包括依赖的protoc和grpc插件)都使用C++17及cxx11 ABI:
    build --cxxopt=-std=c++17
    build --cxxopt=-D_GLIBCXX_USE_CXX11_ABI=1
    build --host_cxxopt=-std=c++17
    build --host_cxxopt=-D_GLIBCXX_USE_CXX11_ABI=1
    
  • 升级依赖版本:当前使用的grpc v1.44.0和rules_proto 4.0.0-3.19.1版本较老,对Apple Silicon支持不完善。建议升级到:
    • rules_proto 5.x系列稳定版
    • grpc v1.50.0及以上版本
  • 清理Bazel缓存:执行以下命令彻底清除旧构建产物,避免干扰:
    bazel clean --expunge
    
  • 明确指定C++工具链:在WORKSPACE中添加rules_cc依赖,强制使用Bazel官方维护的C++工具链:
    http_archive(
        name = "rules_cc",
        sha256 = "e26d8105b5f4f4b9087f45172f9b29a90a075871e945146a66b7e43bcfc7030c",
        strip_prefix = "rules_cc-0.0.9",
        urls = ["https://github.com/bazelbuild/rules_cc/archive/refs/tags/0.0.9.tar.gz"],
    )
    load("@rules_cc//cc:repositories.bzl", "rules_cc_dependencies")
    rules_cc_dependencies()
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 02:08:10