使用带有检查器的Makefile构建clang时出现链接器错误

时间:2013-10-22 14:57:39

标签: macos c++11 linker clang llvm

我在链接阶段遇到了问题。

我想做什么?

我正在尝试为clang添加一个检查器。我从源代码构建了LLVM和libc ++(是的,在构建之后的libcxx测试期间,某些测试失败)。为了识别libc ++库,我将以下代码添加到Clang的根目录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

链接器问题浮出水面如下。

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

我按照链接器的建议使用-v选项来查看链接器调用,并得到以下输出。

"/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

我发现它正在使用我为它构建的C ++ 11库,因为它是上面调用中的第6个选项。还有其他选择我应该照顾吗?

如果有任何解决方案可用,我试着谷歌,我发现在早期版本的llvm中存在这种非内联虚拟功能缺少vtable条目的问题,并且使用最新版本帮助了其他人。但我已经在使用llvm最新版本了。以下是clang -v输出

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

请指导我解决问题的可能方法。如果我需要分享更多细节,请告诉我。如果我没有办法,违反任何惯例或规范(这是我在StackOverflow上的第一个问题),我很抱歉,请告诉我。

请注意:我之前添加了一个检查程序,它工作正常并完成了我的目的。当我尝试包含标准C ++标头&lt; regex&gt;时,所有上述问题都引发了。延长相同的检查程序。

2 个答案:

答案 0 :(得分:2)

正如您在链接器cmdline中看到的那样,您正尝试使用libstdc ++(-lstdc ++)进行链接。因此,您将使用libc ++编译的对象与libstdc ++混合

答案 1 :(得分:1)

问题已解决。使用以下配置选项编译LLVM有助于我解决此问题。

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

可能有一天它会帮助某人。谢谢大家。