我尝试编译以下代码clang 的 libtooling 教程(“拼凑”一节)。编译工作:
g++ `llvm-config-3.5 --cxxflags` -fno-rtti -c test.cpp -o test.o
我从 clang 获得了未定义的引用,因此我甚至尝试链接所有 clang 库:
CLANG_LIBS="$(ls /usr/lib/llvm-3.5/lib/libclang* | sed -e 's/.*lib/-l/g' -e 's/\.a//g' -e 's/\.so.*//g' | tr '\n' ' ' )"
g++ `llvm-config-3.5 --ldflags --libs` $CLANG_LIBS test.o -o test
我得到〜15个未定义的引用:
test.o: In function `ASTFrontendAction':
/usr/lib/llvm-3.5/include/clang/Frontend/FrontendAction.h:215: undefined reference to `clang::FrontendAction::FrontendAction()'
test.o: In function `SyntaxOnlyAction':
/usr/lib/llvm-3.5/include/clang/Frontend/FrontendActions.h:129: undefined reference to `vtable for clang::SyntaxOnlyAction'
test.o: In function `std::unique_ptr<clang::tooling::FrontendActionFactory, std::default_delete<clang::tooling::FrontendActionFactory> > clang::tooling::newFrontendActionFactory<clang::SyntaxOnlyAction>()::SimpleFrontendActionFactory::~SimpleFrontendActionFactory()':
/usr/lib/llvm-3.5/include/clang/Tooling/Tooling.h:309: undefined reference to `clang::tooling::FrontendActionFactory::~FrontendActionFactory()'
test.o: In function `~SimpleFrontendActionFactory':
/usr/lib/llvm-3.5/include/clang/Tooling/Tooling.h:309: undefined reference to `clang::tooling::FrontendActionFactory::~FrontendActionFactory()'
test.o: In function `clang::tooling::ClangTool::~ClangTool()':
/usr/lib/llvm-3.5/include/clang/Tooling/Tooling.h:252: undefined reference to `clang::tooling::ClangTool::clearArgumentsAdjusters()'
test.o: In function `llvm::RefCountedBase<clang::FileManager>::Release() const':
/usr/lib/llvm-3.5/include/llvm/ADT/IntrusiveRefCntPtr.h:55: undefined reference to `clang::FileManager::~FileManager()'
test.o: In function `~ClangTool':
/usr/lib/llvm-3.5/include/clang/Tooling/Tooling.h:252: undefined reference to `clang::tooling::ClangTool::clearArgumentsAdjusters()'
test.o: In function `llvm::RefCountedBase<clang::FileManager>::Release() const':
/usr/lib/llvm-3.5/include/llvm/ADT/IntrusiveRefCntPtr.h:55: undefined reference to `clang::FileManager::~FileManager()'
test.o: In function `main':
/home/t0161834/cprogs/nccheck/test.cpp:24: undefined reference to `clang::tooling::CommonOptionsParser::CommonOptionsParser(int&, char const**, llvm::cl::OptionCategory&, char const*)'
/home/t0161834/cprogs/nccheck/test.cpp:26: undefined reference to `clang::tooling::ClangTool::ClangTool(clang::tooling::CompilationDatabase const&, llvm::ArrayRef<std::string>)'
/home/t0161834/cprogs/nccheck/test.cpp:27: undefined reference to `clang::tooling::ClangTool::run(clang::tooling::ToolAction*)'
test.o: In function `OptionCategory':
/usr/lib/llvm-3.5/include/llvm/Support/CommandLine.h:150: undefined reference to `llvm::cl::OptionCategory::registerCategory()'
test.o: In function `__static_initialization_and_destruction_0':
/home/t0161834/cprogs/nccheck/test.cpp:18: undefined reference to `clang::tooling::CommonOptionsParser::HelpMessage'
/home/t0161834/cprogs/nccheck/test.cpp:18: undefined reference to `llvm::cl::extrahelp::extrahelp(char const*)'
/home/t0161834/cprogs/nccheck/test.cpp:21: undefined reference to `llvm::cl::extrahelp::extrahelp(char const*)'
test.o:(.data.rel.ro._ZTVZN5clang7tooling24newFrontendActionFactoryINS_16SyntaxOnlyActionEEESt10unique_ptrINS0_21FrontendActionFactoryESt14default_deleteIS4_EEvE27SimpleFrontendActionFactory[_ZTVZN5clang7tooling24newFrontendActionFactoryINS_16SyntaxOnlyActionEEESt10unique_ptrINS0_21FrontendActionFactoryESt14default_deleteIS4_EEvE27SimpleFrontendActionFactory]+0x20): undefined reference to `clang::tooling::FrontendActionFactory::runInvocation(clang::CompilerInvocation*, clang::FileManager*, clang::DiagnosticConsumer*)'
collect2: error: ld returned 1 exit status
已安装的软件包:
$ dpkg -l "*clang*" | grep ii | awk '{ print $2 }' | tr '\n' ' '
clang-3.5 libclang-3.5-dev libclang-common-3.5-dev libclang1:amd64 libclang1-3.4:amd64 libclang1-3.5:amd64
$ dpkg -l "*llvm*" | grep ii | awk '{ print $2 }' | tr '\n' ' '
libllvm3.4:amd64 libllvm3.5:amd64 llvm-3.5 llvm-3.5-dev llvm-3.5-runtime
有人可以解决或者至少重现该错误吗?
答案1
cpp 文件和 clang 库的顺序很重要,clang 库的顺序也很重要。我使用以下命令成功链接
g++ `llvm-config --cxxflags --ldflags` tool.cpp $CLANG_LIBS `llvm-config --libs --system-libs` -o tool
其中 CLANG_LIBS 定义为
CLANGLIBS="-lclangTooling -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangARCMigrate -lclangRewrite -lclangRewriteFrontend -lclangEdit -lclangAST -lclangLex -lclangBasic -lclang"