Reputation: 15313
I feel like I'm missing something very simple here. I want to play around with clang, so as a starting point I followed the code example in this video, around 3:40. The code is as follows:
#include "clang-c/Index.h" // Note: These two lines were
#include <stdio.h> // omitted from the video slides
int main(int argc, char *argv[])
{
CXIndex Index = clang_createIndex(0, 0);
CXTranslationUnit TU = clang_parseTranslationUnit(Index, 0, argv, argc, 0, 0, CXTranslationUnit_None);
for (unsigned I = 0, N = clang_getNumDiagnostics(TU); I != N; ++I)
{
CXDiagnostic Diag = clang_getDiagnostic(TU, I);
CXString String = clang_formatDiagnostic(Diag, clang_defaultDiagnosticDisplayOptions());
fprintf(stderr, "%s\n", clang_getCString(String));
clang_disposeString(String);
}
clang_disposeTranslationUnit(TU);
clang_disposeIndex(Index);
return 0;
}
In the video, he does not states that he's omitted the two #include
directives. I think I correctly filled those in in my example above. He also omits how the file was compiled and linked, which is the part I'm having trouble with. Following the instructions here I checked out and compiled clang and llvm. The files were checked out to ~/src/llvm
then compiled from ~/src/build
(just as the instructions say), and now I'm trying to make the simple test project above in ~/src/test
. Below is how I'm calling gcc, and the output.
gcc -I../llvm/tools/clang/include/ -L../build/Debug+Asserts/lib/ -lclang main.cpp -o test
/tmp/ccrpABsq.o: In function `main':
main.cpp:(.text+0x24): undefined reference to `clang_createIndex'
main.cpp:(.text+0x5f): undefined reference to `clang_parseTranslationUnit'
main.cpp:(.text+0x74): undefined reference to `clang_getNumDiagnostics'
main.cpp:(.text+0x8b): undefined reference to `clang_getDiagnostic'
main.cpp:(.text+0x93): undefined reference to `clang_defaultDiagnosticDisplayOptions'
main.cpp:(.text+0xab): undefined reference to `clang_formatDiagnostic'
main.cpp:(.text+0xc0): undefined reference to `clang_getCString'
main.cpp:(.text+0xed): undefined reference to `clang_disposeString'
main.cpp:(.text+0x10d): undefined reference to `clang_disposeTranslationUnit'
main.cpp:(.text+0x118): undefined reference to `clang_disposeIndex'
collect2: ld returned 1 exit status
make: *** [all] Error 1
I checked ~/build/Debut+Asserts/lib
and there is both libclang.a
and libclang.so
in that directory. I'm not sure what I'm doing wrong. I've tried googling around and haven't found any tips, or instructions on what I should be linking against. Regardless, I've tried a few different things and nothing seems to work.
Upvotes: 3
Views: 3321
Reputation: 179392
Put -lclang
after main.cpp
. The order of arguments matters to gcc
, as it resolves symbols from static libraries in order. See also Why does the order in which libraries are linked sometimes cause errors in GCC?.
Upvotes: 5