joepols
September 11, 2024, 11:09pm
1
I am trying to get started with using clang. Here’s my code:
// main.cpp
#include <clang/Basic/LangOptions.h>
int main() {
clang::LangOptions opts;
return 0;
}
I have successfully built clang & llvm from scratch following the guide here :
cmake -DLLVM_ENABLE_PROJECTS=clang \
-DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" \
../llvm && make
This has populated build/libs/
and build/include
with the necessary shared object files and headers (I think I have everything I need here).
Seemingly no matter what flags I use, I always get linker errors when trying to compile & link my main.cpp
file. My compiler seems to find the header files just fine, but still throws a linker error. For example:
g++ main.cpp -o main.out `llvm-config --cxxflags --ldflags --libs`
throws
/usr/bin/ld: /tmp/ccQjPi6o.o: in function `main':
main.cpp:(.text+0x17): undefined reference to `clang::LangOptions::LangOptions()'
collect2: error: ld returned 1 exit status
Even when using --libs all
, I get the same exact linking error. And yes, the llvm-config
binary is from my fresh build.
I have tried both solutions here and here , but nothing seemed to work. Surely I am missing something?
I always assumed llvm-config
meant llvm the overall project not just llvm/
but that shows how much I’ve used it
I tried this locally. I ninja install
’d my build then tried:
$ g++ /tmp/main.cpp -o main.out `/home/david.spickett/stage1.install/bin/llvm-config --cxxflags --ldflags --libs`
/usr/bin/ld: /tmp/ccNZ5rYe.o: in function `main':
main.cpp:(.text+0x28): undefined reference to `clang::LangOptions::LangOptions()'
collect2: error: ld returned 1 exit status
Which is exactly what you got.
Then I tried adding the clang libs on the end, which is what c++ - Linking against clang-llvm - Stack Overflow suggests.
$ g++ /tmp/main.cpp -o main.out `/home/david.spickett/stage1.install/bin/llvm-config --cxxflags --ldflags --libs` -L/home/david.spickett/stage1.install/lib/
-lclangBasic
/usr/bin/ld: /home/david.spickett/stage1.install/lib/libclangBasic.a(LangOptions.cpp.o): in function `clang::LangOptions::getOpenCLCompatibleVersion() const':
LangOptions.cpp:(.text._ZNK5clang11LangOptions26getOpenCLCompatibleVersionEv+0x54): undefined reference to `llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)'
/usr/bin/ld: /home/david.spickett/stage1.install/lib/libclangBasic.a(LangOptions.cpp.o): in function `clang::LangOptions::remapPathPrefix(llvm::SmallVectorImpl<char>&) const':
LangOptions.cpp:(.text._ZNK5clang11LangOptions15remapPathPrefixERN4llvm15SmallVectorImplIcEE+0x34): undefined reference to `llvm::sys::path::replace_path_prefix(llvm::SmallVectorImpl<char>&, llvm::StringRef, llvm::StringRef, llvm::sys::path::Style)'
<...>
And got a lot of errors in relating to llvm libraries.
If I instead put the clang libs first:
$ g++ /tmp/main.cpp -o main.out -L/home/david.spickett/stage1.install/lib/ -lclangBasic `/home/david.spickett/stage1.install/bin/llvm-config --cxxflags --ldflags --libs`
$ $?
-bash: 0: command not found
It works ($?
shows the last return code and 0 is a success).
Does any of that help or are you getting different results?
1 Like