Question

I am using Linux Mint 15 Olivia, and I installed the clang compiler and libclang-dev packages available in the repositories, they are version 3.2.

I am trying to compile and link the example from clang repository http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_32/final/examples/clang-interpreter/main.cpp as you can see it's from clang version 3.2.

When I compiled it with the command:

$> clang++ `llvm-config --cflags` -c main.cpp

I got the file main.o which contains some undefined symbols to clang/llvm libraries. I made sure it contains the symbols by using the nm command:

$> nm main.o

A symbol from clang would be

_ZN5clang16CompilerInstanceC1Ev

Until now everything worked fine, until I tried to link the 'main.o' file with clang/llvm libraries. When I issue the command:

$> clang++ `llvm-config --ldflags` main.o `llvm-config --libs`

It fails with the following output (I just put the first error to not clutter this post):

main.o: In function `main':
main.cpp:74: undefined reference to `clang::TextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream&, clang::DiagnosticOptions*, bool)'

Basically I get a whole bunch of undefined references to clang libraries. I've tried researching on this and all I've found is that this has to do with the order you put the libraries flags on the command line. In addition to this, I also tried some other things:

  1. I tried a bunch of compiler flags, changing the order in which the linker flags appear, they never worked.
  2. I downloaded and compiled the LLVM and clang source code version 3.2, and 3.4, same result: compiling works, linking fails. It's worth mentioning to say that for each version reordering the linker flags always gave different link errors (this is of course due to the way the link searches for libraries).

I ran out of ideas, and already spent 2 hours trying to compile a simple example from clang's repository, any help would be appreciated.

Thank you

Was it helpful?

Solution

The answer is easy - llvm-config will not give you clang libraries. You need to link them separately. Check clang/tools/driver/Makefile as an example of a library list.

OTHER TIPS

The answer to this problem is as Anton Korobeynik suggested, I was missing the clang libraries (which are not part of the llvm build as I was expecting from the command 'llvm-config --libs').

In the end the final command turned out to be:

 clang++ `llvm-config --ldflags` main.o  -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangARCMigrate -lclangRewriteFrontend -lclangRewriteCore -lclangEdit -lclangAST -lclangLex -lclangBasic `llvm-config --libs`

If compiling any clang tool or example, make sure you check the Makefiles under clang/tools folder :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top