Linker error while building clang using Makefile w

2019-04-14 01:07发布

问题:

I'm facing a problem during the linking phase.

What am I trying to do?

I'm trying to add a checker to clang. I built LLVM and libc++ from the source code (Yes, some test failed during libcxx testing after build). To identify the libc++ library, I added the following code to the Clang's root directory Makefile

CXX.Flags += -stdlib=libc++
CXX.Flags += -std=c++11
CXX.Flags += -nostdinc++
CXX.Flags += -I/path/to/my/own/c++library/libcxx/include
LD.Flags += -L/path/to/my/own/c++library/libcxx/lib

The linker problem surfaced as following.

Undefined symbols for architecture x86_64:
"std::__1::__shared_weak_count::__get_deleter(std::type_info const&) const", referenced from:
      vtable for std::__1::__shared_ptr_p-traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::regex_traits<char>::__init() in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::ios_base::getloc() const", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::__get_classname(char const*, bool)", referenced from:
      unsigned int std::__1::regex_traits<char>::__lookup_classname<char const*>(char const*, char const*, bool, char) const in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::__shared_weak_count::__add_shared()", referenced from:
      std::__1::basic_regex<char, std::__1::regex_traits<char> >::basic_regex(std::__1::basic_regex<char, std::__1::regex_traits<char> > const&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::__shared_weak_count::__release_shared()", referenced from:
      std::__1::shared_ptr<std::__1::__empty_state<char> >::~shared_ptr() in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::__shared_weak_count::~__shared_weak_count()", referenced from:
      std::__1::__shared_ptr_pointer<std::__1::__empty_state<char>*, std::__1::default_delete<std::__1::__empty_state<char> >, std::__1::allocator<std::__1::__empty_state<char> > >::~__shared_ptr_pointer() in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::__get_collation_name(char const*)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > std::__1::regex_traits<char>::__lookup_collatename<char const*>(char const*, char const*, char) const in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > std::__1::regex_traits<char>::__lookup_collatename<char*>(char*, char*, char) const in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::cout", referenced from:
      (anonymous namespace)::ObjCSelfInitChecker::checkPreStmt(clang::ObjCStringLiteral const*, clang::ento::CheckerContext&) const in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::ctype<char>::id", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::regex_traits<char>::__init() in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::locale::locale(std::__1::locale const&)", referenced from:
      std::__1::basic_regex<char, std::__1::regex_traits<char> >::__start_matching_list(bool) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::regex_traits<char>::regex_traits(std::__1::regex_traits<char> const&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::locale::locale()", referenced from:
      std::__1::regex_traits<char>::regex_traits() in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::locale::~locale()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::regex_traits<char>::~regex_traits() in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_regex<char, std::__1::regex_traits<char> >::__start_matching_list(bool) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::collate<char>::id", referenced from:
      std::__1::regex_traits<char>::__init() in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "std::__1::ios_base::clear(unsigned int)", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush() in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry() in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  "vtable for std::__1::__shared_count", referenced from:
      std::__1::shared_ptr<std::__1::__empty_state<char> >::shared_ptr<std::__1::__empty_state<char>, void>(std::__1::__empty_state<char>*) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for std::__1::__shared_weak_count", referenced from:
      std::__1::shared_ptr<std::__1::__empty_state<char> >::shared_ptr<std::__1::__empty_state<char>, void>(std::__1::__empty_state<char>*) in libclangStaticAnalyzerCheckers.a(ObjCSelfInitChecker.o)
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [/Users/msecurity/Documents/Deepak/Eclipse/clang_build/build/Debug+Asserts/bin/clang] Error 1
make[1]: *** [driver/.makeall] Error 2
make: *** [all] Error 1

I followed the linker's suggestion to use -v option to see linker invocation, and got the following output.

"/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o /path/to/onw/clang/build/Debug+Asserts/bin/clang -L/path/to/onw/clang//build/Debug+Asserts/lib -L/path/to/onw/clang/build/Debug+Asserts/lib -L/path/to/libc++/build/libcxx/lib -rpath @executable_path/../lib -sectcreate __TEXT __info_plist /path/to/onw/clang/build/tools/clang/tools/driver/Debug+Asserts/Info.plist /path/to/onw/clang/build/tools/clang/tools/driver/Debug+Asserts/cc1_main.o /path/to/onw/clang/build/tools/clang/tools/driver/Debug+Asserts/cc1as_main.o /path/to/onw/clang/build/tools/clang/tools/driver/Debug+Asserts/driver.o -lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangARCMigrate -lclangRewriteFrontend -lclangRewriteCore -lclangAnalysis -lclangEdit -lclangAST -lclangBasic -lclangLex -lLLVMOption -lLLVMLinker -lLLVMIRReader -lLLVMipo -lLLVMVectorize -lLLVMInstrumentation -lLLVMBitWriter -lLLVMBitReader -lLLVMAsmParser -lLLVMR600CodeGen -lLLVMR600Desc -lLLVMR600Info -lLLVMR600AsmPrinter -lLLVMSystemZDisassembler -lLLVMSystemZCodeGen -lLLVMSystemZAsmParser -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSystemZAsmPrinter -lLLVMHexagonCodeGen -lLLVMHexagonAsmPrinter -lLLVMHexagonDesc -lLLVMHexagonInfo -lLLVMNVPTXCodeGen -lLLVMNVPTXDesc -lLLVMNVPTXInfo -lLLVMNVPTXAsmPrinter -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMMSP430CodeGen -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMSP430AsmPrinter -lLLVMXCoreDisassembler -lLLVMXCoreCodeGen -lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMXCoreAsmPrinter -lLLVMMipsDisassembler -lLLVMMipsCodeGen -lLLVMMipsAsmParser -lLLVMMipsDesc -lLLVMMipsInfo -lLLVMMipsAsmPrinter -lLLVMARMDisassembler -lLLVMARMCodeGen -lLLVMARMAsmParser -lLLVMARMDesc -lLLVMARMInfo -lLLVMARMAsmPrinter -lLLVMAArch64Disassembler -lLLVMAArch64CodeGen -lLLVMAArch64AsmParser -lLLVMAArch64Desc -lLLVMAArch64Info -lLLVMAArch64AsmPrinter -lLLVMAArch64Utils -lLLVMPowerPCCodeGen -lLLVMPowerPCAsmParser -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMPowerPCAsmPrinter -lLLVMSparcCodeGen -lLLVMSparcDesc -lLLVMSparcInfo -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMMCParser -lLLVMCodeGen -lLLVMObjCARCOpts -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMX86Desc -lLLVMX86Info -lLLVMTarget -lLLVMX86AsmPrinter -lLLVMMC -lLLVMObject -lLLVMX86Utils -lLLVMCore -lLLVMSupport -lz -lpthread -lcurses -lm -lstdc++ -lSystem /usr/bin/../lib/clang/4.2/lib/darwin/libclang_rt.osx.a

I observed that it's using the C++11 library that I built for it, as it is the 6th option to ld in above invocation. Is there any other options that I should take care of ?

I tried to google out if any solution is available, and I found this kind of problem of missing vtable entries for non-inline virtual function existed in the earlier versions of llvm, and using the latest version helped others. But I'm already using llvm latest version. The following is the clang -v output

clang version 3.4 (trunk 192102) (llvm/trunk 192100)
Target: x86_64-apple-darwin12.3.0
Thread model: posix

Please guide me with the possible way to solve the problem. Let me know if I need to share any more details. If I'm any way, violating any conventions or norms(this is my first question on StackOverflow), I'm sorry for that, and kindly let me know.

Please Note: I added a checker earlier and it was working fine and fulfilled my purpose. All the above problem raised when I tried to include standard C++ header <regex> to extend the same checker.

回答1:

As you can see in the linker cmdline, you're trying to link using libstdc++ (-lstdc++). So, you're mixing the objects compiled with libc++ with libstdc++



回答2:

Problem resolved. Compiling LLVM with following configuration options helped me to resolve the issue.

../llvm/configure \ CXXFLAGS="-stdlib=libc++ -std=c++11" \ OBJCXXFLAGS="-stdlib=libc++ -std=c++11" \ LDFLAGS="-stdlib=libc++"

May be it will help someone someday. Thank you all.