Search code examples
c++llvmclangllvm-clang

Linking against clang-llvm


I've been working on a small tool with clang/llvm but I haven't been able to successfully get g++ and gnu's linker to properly link my code against clang.

my linker is generating the following errors:

undefined reference to `clang::FileManager::~FileManager()'

undefined reference to `clang::FileManager::FileManager()'

undefined reference to `llvm::sys::getHostTriple()'

undefined reference to `clang::Diagnostic::Diagnostic(clang::DiagnosticClient*)'

undefined reference to `llvm::outs()'

undefined reference to `clang::TargetInfo::CreateTargetInfo(clang::Diagnostic&, clang::TargetOptions&)'

undefined reference to `clang::SourceManager::getOrCreateContentCache(clang::FileEntry const*)'

undefined reference to `clang::SourceManager::createFileID(clang::SrcMgr::ContentCache const*, clang::SourceLocation, clang::SrcMgr::CharacteristicKind, unsigned int, unsigned int)'

my compile commands looks like this:

g++ -g -fno-rtti -I~/llvm-2.8/tools/clang-2.8/include \
  -I~/llvm-2.8/llvm/include \
  `~/bin/llvm-config --cxxflags` \
  -c Frontend.cpp

g++ -g -fno-rtti -I~/llvm-2.8/tools/clang-2.8/include \
  -I~/llvm-2.8/llvm/include \
  `~/bin/llvm-config --cxxflags` \
  -c exec.cpp

g++ -I~/llvm-2.8/tools/clang-2.8/include \
    -I~/llvm-2.8/llvm/include -L~/opt/lib/ \
    -g -fno-rtti -lclangDriver -lclangAnalysis \
    -lclangFrontend -lclangSema -lclangAST -lclangParse \
    -lclangLex -lclangBasic  \
    `~/bin/llvm-config --cxxflags --ldflags --libs`  \
    Frontend.o exec.o -o run

any tips or advice would be welcomed.

cheers, ct

PS: I've been exploring some of the information on this page:

http://ubuntuforums.org/showthread.php?t=532693

and it might do the trick, will post a comment on that tip when I can.

Solution

using clang code from this tutorial (which had to be modified to remove the references to FileSystemOptions b/c clang/Basic/FileSystemOptions.h doesn't exist in clang-2.8): http://clangtutorial.codeplex.com/

g++ tutorial1.cpp -g -fno-rtti -lclangFrontend -lclangDriver       \
    -lclangCodeGen -lclangSema -lclangChecker -lclangAnalysis      \
    -lclangRewrite -lclangAST -lclangParse -lclangLex -lclangBasic \
    -lLLVMSupport -lLLVMSystem -I~/opt/include/                    \
    `llvm-config --cxxflags --ldflags --libs all`

seemed to do the trick!


Solution

  • When I've built some stuff against llvm / clang, this is what I've used to build it. Perhaps you can compare the two build lines.

    Also, the llvm-config command I've used has been: llvm-config --cxxflags --ldflags --libs backend.

    Finally, this is likely partially related to an ordering issue. You probably want to include the libraries for llvm before you include the clang libraries.

    /usr/bin/g++                                                              \
        -fno-exceptions -fno-rtti -fno-common                                 \
        -I/Users/wlynch/Homebrew/include                                      \
        -DNDEBUG -D_GNU_SOURCE -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS \
        ../src/main.cpp -c -o src/main.cpp.0.o
    
    /usr/bin/g++
         src/main.cpp.0.o -o /Users/wlynch/Dropbox/Clang/Indexer/build/main               \
         -L/Users/wlynch/Homebrew/lib -L/Users/wlynch/Homebrew/lib                        \
         -lpthread -lm                                                                    \
         -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG     \
         -lLLVMAsmPrinter -lLLVMMCParser -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine \
         -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMCore            \
         -lLLVMX86AsmPrinter -lLLVMMC -lLLVMX86Info -lLLVMSupport -lLLVMSystem            \
         -lclangAST -lclangAnalysis -lclangBasic -lclangChecker -lclangCodeGen            \
         -lclangDriver -lclangFrontend -lclangFrontendTool -lclangIndex -lclangLex        \
         -lclangParse -lclangRewrite -lclangSema -lclangSerialization