My clang code doesn't linked

This is my code:

#include "clang/Driver/Options.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"

using namespace std;
using namespace clang;
using namespace clang::driver;
using namespace clang::tooling;
using namespace llvm;

Rewriter rewriter;
int numFunctions = 0;

int main(int argc, const char **argv) {
    llvm::Expected<CommonOptionsParser> op = CommonOptionsParser::create(argc, argv, llvm::cl::getGeneralCategory()); 
}

this is my clang version (built from source):

~/Projects/compiler/llvm-project-llvmorg-14.0.1/build/bin/clang -v
clang version 14.0.1
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: ~/Projects/compiler/llvm-project-llvmorg-14.0.1/build/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Selected multilib: .;@m64

and this is my compile command:

~/Projects/compiler/llvm-project-llvmorg-14.0.1/build/bin/clang -L/usr/lib  -lclangAST -lclangBasic -lclangFrontend -lclangSerialization -lclangTooling -lclangFrontend -lclangSerialization -lclangDriver -lclangTooling -lclangParse -lclangSema -lclangAnalysis -lclangRewriteFrontend -lclangRewrite -lclangEdit -lclangLex -lclangBasic test.cpp -o test 

All libraries are located in /usr/lib

this is my compile result:

/usr/bin/ld: /tmp/test-f7efbc.o: in function `main':
test.cpp:(.text+0x18): undefined reference to `llvm::cl::getGeneralCategory()'
/usr/bin/ld: test.cpp:(.text+0x37): undefined reference to `clang::tooling::CommonOptionsParser::create(int&, char const**, llvm::cl::OptionCategory&, llvm::cl::NumOccurrencesFlag, char const*)'
/usr/bin/ld: /tmp/test-f7efbc.o: in function `clang::ConstructionContextItem::getKindAsString(clang::ConstructionContextItem::ItemKind)':
test.cpp:(.text._ZN5clang23ConstructionContextItem15getKindAsStringENS0_8ItemKindE[_ZN5clang23ConstructionContextItem15getKindAsStringENS0_8ItemKindE]+0x11c): undefined reference to `llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)'
/usr/bin/ld: /tmp/test-f7efbc.o: in function `__clang_call_terminate':
test.cpp:(.text.__clang_call_terminate[__clang_call_terminate]+0x2): undefined reference to `__cxa_begin_catch'
/usr/bin/ld: test.cpp:(.text.__clang_call_terminate[__clang_call_terminate]+0x7): undefined reference to `std::terminate()'
/usr/bin/ld: /tmp/test-f7efbc.o: in function `clang::RewriteBuffer::~RewriteBuffer()':
test.cpp:(.text._ZN5clang13RewriteBufferD2Ev[_ZN5clang13RewriteBufferD2Ev]+0x22): undefined reference to `clang::DeltaTree::~DeltaTree()'
/usr/bin/ld: /tmp/test-f7efbc.o: in function `clang::RewriteRope::~RewriteRope()':
test.cpp:(.text._ZN5clang11RewriteRopeD2Ev[_ZN5clang11RewriteRopeD2Ev]+0x22): undefined reference to `clang::RopePieceBTree::~RopePieceBTree()'
/usr/bin/ld: /tmp/test-f7efbc.o: in function `clang::RopeRefCountString::Release()':
test.cpp:(.text._ZN5clang18RopeRefCountString7ReleaseEv[_ZN5clang18RopeRefCountString7ReleaseEv]+0x94): undefined reference to `operator delete[](void*)'
/usr/bin/ld: /tmp/test-f7efbc.o: in function `__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<clang::FileID const, clang::RewriteBuffer> > >::deallocate(std::_Rb_tree_node<std::pair<clang::FileID const, clang::RewriteBuffer> >*, unsigned long)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKN5clang6FileIDENS3_13RewriteBufferEEEE10deallocateEPS8_m[_ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKN5clang6FileIDENS3_13RewriteBufferEEEE10deallocateEPS8_m]+0x19): undefined reference to `operator delete(void*)'
/usr/bin/ld: /tmp/test-f7efbc.o: in function `void std::_Destroy<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
test.cpp:(.text._ZSt8_DestroyINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEvPT_[_ZSt8_DestroyINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEvPT_]+0x11): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/usr/bin/ld: /tmp/test-f7efbc.o: in function `__gnu_cxx::new_allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::deallocate(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, unsigned long)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE10deallocateEPS6_m[_ZN9__gnu_cxx13new_allocatorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE10deallocateEPS6_m]+0x19): undefined reference to `operator delete(void*)'
/usr/bin/ld: /tmp/test-f7efbc.o:(.data+0x0): undefined reference to `llvm::DisableABIBreakingChecks'
/usr/bin/ld: /tmp/test-f7efbc.o:(.eh_frame+0xa6b): undefined reference to `__gxx_personality_v0'
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

why? what’s wrong? I’m so confused and I hope you can help!
thank you!

Any Guides??

If you invoke clang as “clang”, and not “clang++”, it doesn’t link the C++ standard library. That explains most of the missing symbols, I think.

I test it with clang++ and libLLVMSupport.a but nothing changed.
what should I do?

“Nothing changed”, as in you’re still getting an error about __cxa_begin_catch? That doesn’t make any sense.

If you invoke “clang++”, it will add the C++ standard library to the command. (You can make clang dump the link command using “-###”.) If the C++ standard library is passed to the link command, either it will find the library, or print an error. If it finds the library, the library will provide __gxx_personality_v0. I can’t think of any way for that to go wrong.

Yes I mean nothing special changed.
here is the console output:

/usr/bin/ld: /tmp/test-abb233.o: in function `main':
test.cpp:(.text+0x18): undefined reference to `llvm::cl::getGeneralCategory()'
/usr/bin/ld: test.cpp:(.text+0x37): undefined reference to `clang::tooling::CommonOptionsParser::create(int&, char const**, llvm::cl::OptionCategory&, llvm::cl::NumOccurrencesFlag, char const*)'
/usr/bin/ld: /tmp/test-abb233.o: in function `clang::ConstructionContextItem::getKindAsString(clang::ConstructionContextItem::ItemKind)':
test.cpp:(.text._ZN5clang23ConstructionContextItem15getKindAsStringENS0_8ItemKindE[_ZN5clang23ConstructionContextItem15getKindAsStringENS0_8ItemKindE]+0x11c): undefined reference to `llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)'
/usr/bin/ld: /tmp/test-abb233.o: in function `clang::RewriteBuffer::~RewriteBuffer()':
test.cpp:(.text._ZN5clang13RewriteBufferD2Ev[_ZN5clang13RewriteBufferD2Ev]+0x22): undefined reference to `clang::DeltaTree::~DeltaTree()'
/usr/bin/ld: /tmp/test-abb233.o: in function `clang::RewriteRope::~RewriteRope()':
test.cpp:(.text._ZN5clang11RewriteRopeD2Ev[_ZN5clang11RewriteRopeD2Ev]+0x22): undefined reference to `clang::RopePieceBTree::~RopePieceBTree()'
/usr/bin/ld: /tmp/test-abb233.o:(.data+0x0): undefined reference to `llvm::DisableABIBreakingChecks'

From the undefined reference to llvm::cl::getGeneralCategory(), my best guess is that you’re linking against libraries from an old version of LLVM, somehow.

all of my library are built from last version of llvm project and the clang++ bin file also built from the same

Oh, I see what’s happening. If you’re using the GNU linker, the order you list files in the command matters. If you put -lLLVMSupport before test.cpp, then test.cpp can’t refer to symbols from libLLVMSupport.a. Therefore, you get a link error.

leads to this:

/usr/bin/ld: /tmp/test-4e6eb9.o: in function `main':
test.cpp:(.text+0x37): undefined reference to `clang::tooling::CommonOptionsParser::create(int&, char const**, llvm::cl::OptionCategory&, llvm::cl::NumOccurrencesFlag, char const*)'
/usr/bin/ld: /tmp/test-4e6eb9.o:(.data+0x0): undefined reference to `llvm::DisableABIBreakingChecks'
/usr/bin/ld: /home/rdwn/Documents/projs/safeC/libraries/clang//libLLVMSupport.a(Process.cpp.o): in function `terminalHasColors(int)':
/home/rdwn/Projects/compiler/llvm-project-llvmorg-14.0.1/llvm/lib/Support/Unix/Process.inc:339: undefined reference to `set_curterm'
/usr/bin/ld: /home/rdwn/Projects/compiler/llvm-project-llvmorg-14.0.1/llvm/lib/Support/Unix/Process.inc:341: undefined reference to `setupterm'
/usr/bin/ld: /home/rdwn/Projects/compiler/llvm-project-llvmorg-14.0.1/llvm/lib/Support/Unix/Process.inc:359: undefined reference to `tigetnum'
/usr/bin/ld: /home/rdwn/Projects/compiler/llvm-project-llvmorg-14.0.1/llvm/lib/Support/Unix/Process.inc:363: undefined reference to `set_curterm'
/usr/bin/ld: /home/rdwn/Projects/compiler/llvm-project-llvmorg-14.0.1/llvm/lib/Support/Unix/Process.inc:364: undefined reference to `del_curterm'
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

I added -ltinfo and this is the result:

/usr/bin/ld: /tmp/test-02553d.o: in function `main':
test.cpp:(.text+0x37): undefined reference to `clang::tooling::CommonOptionsParser::create(int&, char const**, llvm::cl::OptionCategory&, llvm::cl::NumOccurrencesFlag, char const*)'
/usr/bin/ld: /tmp/test-02553d.o:(.data+0x0): undefined reference to `llvm::DisableABIBreakingChecks'
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

Finally I found the solution:

For building object file:

g++ -fcompare-debug-second -w -c -I/usr/lib/llvm-14/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS  -Wall -Wextra -g -I/usr/lib/llvm-14/include -std=c++14   -fno-exceptions -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -fno-rtti test.cpp

For linking:

g++ -fcompare-debug-second -w -o test -Wall -Wextra -g -I/usr/lib/llvm-14/include -std=c++14   -fno-exceptions -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -fno-rtti -L/usr/lib/llvm-14/lib  test.o -Wl,--start-group -lclang -lclangFrontend -lclangDriver -lclangSerialization -lclangParse -lclangSema -lclangAnalysis -lclangEdit -lclangAST -lclangLex -lclangBasic -lclangTooling -Wl,--end-group -lLLVM-14 

Thanks to everyone who helped