5

I tried to compile the code from clang's libtooling tutorial (section "Put it together"). Compiling works:

g++ `llvm-config-3.5 --cxxflags` -fno-rtti  -c test.cpp -o test.o

I got undefined references from clang, so I even tried linking against all clang libraries:

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

I get ~ 15 undefined references:

    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

Installed packages:

$ 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

Can someone solve or at least reproduce the error?

3
  • Hey, I have the same problem and I'm looking into it as well. Will let you know if something comes up.
    – stanm
    Feb 15, 2016 at 14:44
  • I actually had a different error (had to compile with '-fno-rtti') and after I solved it, I can now compile the example. I'm using llvm-3.7.1, are you sure the tutorial is supposed to work on 3.5?
    – stanm
    Feb 15, 2016 at 15:33
  • @stanm Thanks, but since it's just a linker error, I guess it should work on 3.5. Also, 3.5 is not really old (2014).
    – Johannes
    Feb 15, 2016 at 20:35

1 Answer 1

9

The order of the cpp file and the clang libraries matters, as well as the order of clang libraries. I made it successfully link by using

g++ `llvm-config --cxxflags --ldflags` tool.cpp $CLANG_LIBS `llvm-config --libs --system-libs` -o tool

where CLANG_LIBS was defined as

CLANGLIBS="-lclangTooling -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangARCMigrate -lclangRewrite -lclangRewriteFrontend -lclangEdit -lclangAST -lclangLex -lclangBasic -lclang"
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .