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

从源码构建LLVM/Clang后遭遇大量链接错误求助

LLVM源码构建后链接Clang/LLVM库出现未定义引用问题

构建过程

从LLVM单仓库源码构建clang、llvm、lld、openmp,使用命令:

cmake -G "Ninja" -DLLVM_ENABLE_PROJECTS="clang;lld;llvm;openmp" -DCMAKE_BUILD_TYPE=Debug -DLLVM_USE_LINKER=lld ../llvm -DCMAKE_INSTALL_PREFIX=~/git_repos/llvm-project/install

cmake --build . --target install

测试代码与编译尝试

测试代码依赖以下头文件:

#include "clang/Frontend/ASTUnit.h"                                                        
#include "clang/Serialization/ASTWriter.h"                                                 
#include "clang/Serialization/InMemoryModuleCache.h"                                       
#include "clang/Tooling/Tooling.h"                                                         
#include "llvm/ADT/SmallString.h"                                                          
#include "llvm/Bitstream/BitstreamWriter.h"

编译命令:

g++ -I clang/include -I build/tools/clang/include -I build/include -I llvm/include test.cc $CLANGLIBS -lLLVM-14 -L build/lib

其中$CLANGLIBS变量设置为:

-lclangTooling -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangARCMigrate -lclangRewrite -lclangRewriteFrontend -lclangEdit -lclangAST -lclangLex -lclangBasic -lcurses

错误信息

执行编译命令后抛出大量未定义引用错误,示例:

/usr/bin/ld: build/lib/libclangTooling.a(AllTUsExecution.cpp.o):(.data.rel+0x0): undefined reference to `llvm::EnableABIBreakingChecks'
/usr/bin/ld: build/lib/libclangTooling.a(AllTUsExecution.cpp.o): in function `llvm::Error::assertIsChecked()':
/home/rajkumar/git_repos/llvm-project/llvm/include/llvm/Support/Error.h:269: undefined reference to `llvm::Error::fatalUncheckedError() const'
/usr/bin/ld: build/lib/libclangTooling.a(ArgumentsAdjusters.cpp.o):(.data.rel+0x0): undefined reference to `llvm::EnableABIBreakingChecks'
/usr/bin/ld: build/lib/libclangTooling.a(Execution.cpp.o): in function `clang::tooling::internal::createExecutorFromCommandLineArgsImpl(int&, char const**, llvm::cl::OptionCategory&, char const*)':
/home/rajkumar/git_repos/llvm-project/clang/lib/Tooling/Execution.cpp:76: undefined reference to `llvm::toString[abi:cxx11](llvm::Error)'

解决方案

1. 使用本地构建的llvm-config获取参数

不要手动指定-lLLVM-14,改用自己构建的llvm-config工具生成完整编译、链接参数,确保路径指向本地安装版本:

# 替换为你的llvm-config实际路径
LLVM_CONFIG=~/git_repos/llvm-project/install/bin/llvm-config

2. 修正编译命令的参数顺序

链接器按顺序处理库,依赖库需放在被依赖库之后(Clang库依赖LLVM库,因此LLVM参数要后置)。正确编译命令示例:

g++ $($LLVM_CONFIG --cxxflags) -I clang/include -I build/tools/clang/include test.cc -o test $CLANGLIBS $($LLVM_CONFIG --libs --system-libs --ldflags)

3. 参数说明

  • --cxxflags:获取LLVM要求的编译选项(如宏定义、C++标准版本等),Debug构建下会自动包含llvm::EnableABIBreakingChecks相关定义。
  • --libs:获取所有LLVM核心库的链接参数,替代手动指定的-lLLVM-14,避免遗漏依赖子库。
  • --system-libs:获取LLVM依赖的系统库(如pthread、zlib等)。
  • --ldflags:获取必要的链接器选项(如库路径、链接脚本等)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 11:37:45