使用Bazel构建Carbon时Clang/LLVM报错的解决求助
解决Carbon-Lang构建时ld.lld无法找到libc++与libunwind.a的问题
问题描述
尝试构建Carbon-Lang,已完成准备步骤且Clang/LLVM可正常使用,但执行命令bazel run //explorer -- ./explorer/testdata/print/format_only.carbon时链接external/m4_v1.4.18/bin/m4失败,报错ld.lld找不到-lc++库与libunwind.a库。使用Arch Linux,曾将LLVM路径/usr/include/llvm添加到环境变量但无效,llvm-config --version可正常执行但which llvm无结果。
报错详情:
[bret@archxbret carbon-lang]$ bazel run //explorer -- ./explorer/testdata/print/format_only.carbon Starting local Bazel server and connecting to it... INFO: Invocation ID: 6fb0bb2c-cc78-4fb0-8e35-90759b75109d INFO: Analyzed target //explorer:explorer (67 packages loaded, 1567 targets configured). INFO: Found 1 target... ERROR: /home/bret/.cache/bazel/_bazel_bret/e6835bf0f6859c4bbbb42f026d02cce5/external/m4_v1.4.18/bin/BUILD.bazel:2:10: Linking external/m4_v1.4.18/bin/m4 (for host) failed: (Exit 1): clang++ failed: error executing command /usr/bin/clang++ @bazel-out/host/bin/external/m4_v1.4.18/bin/m4-2.params Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging ld.lld: error: unable to find library -lc++ ld.lld: error: unable to find library -l:libunwind.a ld.lld: error: unable to find library -l:libunwind.a clang++: error: linker command failed with exit code 1 (use -v to see invocation) Target //explorer:explorer failed to build Use --verbose_failures to see the command lines of failed build steps. INFO: Elapsed time: 29.584s, Critical Path: 0.68s INFO: 2 processes: 2 internal. FAILED: Build did NOT complete successfully FAILED: Build did NOT complete successfully
解决方案
1. 安装缺失的依赖包
Arch Linux默认未预装Clang专用的C++标准库libc++和 unwind 库libunwind,执行以下命令安装:
sudo pacman -S libc++ libunwind
2. 配置Bazel使用libc++标准库
Carbon的Bazel构建配置默认依赖libc++,但Arch Linux系统默认C++标准库是libstdc++,需要显式指定构建参数:
- 临时生效:直接在构建命令中追加参数
bazel run //explorer --cxxopt=-stdlib=libc++ --linkopt=-stdlib=libc++ --linkopt=-lunwind -- ./explorer/testdata/print/format_only.carbon - 永久生效:在Carbon-Lang项目根目录创建
.bazelrc文件,添加以下内容,后续构建无需重复输入参数:build --action_env=CC=/usr/bin/clang build --action_env=CXX=/usr/bin/clang++ build --cxxopt=-stdlib=libc++ build --linkopt=-stdlib=libc++ build --linkopt=-lunwind
3. 修正环境变量的错误配置
你之前将/usr/include/llvm添加到PATH是错误操作:PATH是可执行文件的搜索路径,LLVM的可执行命令(如clang、llvm-config)都在系统默认的/usr/bin路径下,无需额外添加。which llvm无结果是因为LLVM没有名为llvm的可执行命令,属于正常现象,无需处理。
内容的提问来源于stack exchange,提问作者Bretwalda
相关产品推荐
相关产品推荐

