Link error on Linux

I’m trying to link a program with LLVM on Linux. I’ve managed to take out the test files from ‘llvm-config --libs’ but now it’s getting errors with missing symbols. The same program successfully links with the same version of LLVM (3.8) on Windows, so it’s not a problem with the code per se, it’s some kind of configuration issue. Any ideas?

g++ -std=c++11 -I/home/a/llvm/include -I/home/a/build/include -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -g -O2 -L/home/a/build//lib -lLLVMLTO -lLLVMObjCARCOpts -lLLVMSymbolize -lLLVMDebugInfoPDB -lLLVMDebugInfoDWARF -lLLVMXCoreDisassembler -lLLVMXCoreCodeGen -lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMXCoreAsmPrinter -lLLVMSystemZDisassembler -lLLVMSystemZCodeGen -lLLVMSystemZAsmParser -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSystemZAsmPrinter -lLLVMSparcDisassembler -lLLVMSparcCodeGen -lLLVMSparcAsmParser -lLLVMSparcDesc -lLLVMSparcInfo -lLLVMSparcAsmPrinter -lLLVMPowerPCDisassembler -lLLVMPowerPCCodeGen -lLLVMPowerPCAsmParser -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMPowerPCAsmPrinter -lLLVMNVPTXCodeGen -lLLVMNVPTXDesc -lLLVMNVPTXInfo -lLLVMNVPTXAsmPrinter -lLLVMMSP430CodeGen -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMSP430AsmPrinter -lLLVMMipsDisassembler -lLLVMMipsCodeGen -lLLVMMipsAsmParser -lLLVMMipsDesc -lLLVMMipsInfo -lLLVMMipsAsmPrinter -lLLVMHexagonDisassembler -lLLVMHexagonCodeGen -lLLVMHexagonAsmParser -lLLVMHexagonDesc -lLLVMHexagonInfo -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMBPFCodeGen -lLLVMBPFDesc -lLLVMBPFInfo -lLLVMBPFAsmPrinter -lLLVMARMDisassembler -lLLVMARMCodeGen -lLLVMARMAsmParser -lLLVMARMDesc -lLLVMARMInfo -lLLVMARMAsmPrinter -lLLVMAMDGPUCodeGen -lLLVMAMDGPUAsmParser -lLLVMAMDGPUDesc -lLLVMAMDGPUUtils -lLLVMAMDGPUInfo -lLLVMAMDGPUAsmPrinter -lLLVMAArch64Disassembler -lLLVMAArch64CodeGen -lLLVMAArch64AsmParser -lLLVMAArch64Desc -lLLVMAArch64Info -lLLVMAArch64AsmPrinter -lLLVMAArch64Utils -lLLVMMIRParser -lLLVMLibDriver -lLLVMOption -lLLVMTableGen -lLLVMLineEditor -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Desc -lLLVMMCDisassembler -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMMCJIT -lLLVMPasses -lLLVMipo -lLLVMVectorize -lLLVMLinker -lLLVMIRReader -lLLVMAsmParser -lLLVMDebugInfoCodeView -lLLVMInterpreter -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMInstrumentation -lLLVMProfileData -lLLVMBitWriter -lLLVMOrcJIT -lLLVMTransformUtils -lLLVMExecutionEngine -lLLVMTarget -lLLVMAnalysis -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCParser -lLLVMBitReader -lLLVMMC -lLLVMCore -lLLVMSupport -ldl -lpthread -ltinfo -o aklo aklo-aklo.o
aklo-aklo.o: In function operator()': /mnt/a2/aklo.cpp:442: undefined reference to llvm::outs()’
aklo-aklo.o: In function llvm::raw_ostream::operator<<(llvm::StringRef)': /home/a/llvm/include/llvm/Support/raw_ostream.h:166: undefined reference to llvm::raw_ostream::write(char const*, unsigned long)’
aklo-aklo.o: In function operator()': /mnt/a2/aklo.cpp:443: undefined reference to llvm::outs()’
/mnt/a2/aklo.cpp:447: undefined reference to llvm::outs()' /mnt/a2/aklo.cpp:450: undefined reference to llvm::outs()’
/mnt/a2/aklo.cpp:452: undefined reference to llvm::outs()' aklo-aklo.o: In function llvm::raw_ostream::operator<<(char const*)’:
/home/a/llvm/include/llvm/Support/raw_ostream.h:166: undefined reference to llvm::raw_ostream::write(char const*, unsigned long)' aklo-aklo.o: In function llvm::raw_ostream::operator<<(llvm::StringRef)’:
/home/a/llvm/include/llvm/Support/raw_ostream.h:166: undefined reference to llvm::raw_ostream::write(char const*, unsigned long)' /home/a/llvm/include/llvm/Support/raw_ostream.h:166: undefined reference to llvm::raw_ostream::write(char const*, unsigned long)’
aklo-aklo.o: In function operator()': /mnt/a2/aklo.cpp:430: undefined reference to llvm::errs()’
aklo-aklo.o: In function llvm::raw_ostream::operator<<(char)': /home/a/llvm/include/llvm/Support/raw_ostream.h:141: undefined reference to llvm::raw_ostream::write(unsigned char)’
aklo-aklo.o: In function error(std::string)': /mnt/a2/aklo.cpp:172: undefined reference to llvm::SourceMgr::PrintMessage(llvm::SMLoc, llvm::SourceMgr::DiagKind, llvm::Twine const&, llvm::ArrayRefllvm::SMRange, llvm::ArrayRefllvm::SMFixIt, bool) const’
aklo-aklo.o: In function print(long)': /mnt/a2/aklo.cpp:388: undefined reference to llvm::outs()’
/mnt/a2/aklo.cpp:393: undefined reference to llvm::outs()' aklo-aklo.o: In function llvm::raw_ostream::operator<<(char)’:
/home/a/llvm/include/llvm/Support/raw_ostream.h:141: undefined reference to llvm::raw_ostream::write(unsigned char)' aklo-aklo.o: In function print(long)’:
/mnt/a2/aklo.cpp:395: undefined reference to llvm::outs()' /mnt/a2/aklo.cpp:401: undefined reference to llvm::outs()’
/mnt/a2/aklo.cpp:398: undefined reference to llvm::outs()' aklo-aklo.o: In function llvm::raw_ostream::operator<<(char)’:
/home/a/llvm/include/llvm/Support/raw_ostream.h:141: undefined reference to llvm::raw_ostream::write(unsigned char)' aklo-aklo.o: In function llvm::StringMap<long, llvm::MallocAllocator>::find(llvm::StringRef) const’:
/home/a/llvm/include/llvm/ADT/StringMap.h:284: undefined reference to llvm::StringMapImpl::FindKey(llvm::StringRef) const' aklo-aklo.o: In function llvm::StringMap<long, llvm::MallocAllocator>::insert(std::pair<llvm::StringRef, long>)’:
/home/a/llvm/include/llvm/ADT/StringMap.h:331: undefined reference to llvm::StringMapImpl::LookupBucketFor(llvm::StringRef)' /home/a/llvm/include/llvm/ADT/StringMap.h:344: undefined reference to llvm::StringMapImpl::RehashTable(unsigned int)’
aklo-aklo.o: In function llvm::raw_ostream::operator<<(char const*)': /home/a/llvm/include/llvm/Support/raw_ostream.h:166: undefined reference to llvm::raw_ostream::write(char const*, unsigned long)’
aklo-aklo.o: In function llvm::raw_ostream::operator<<(std::string const&)': /home/a/llvm/include/llvm/Support/raw_ostream.h:184: undefined reference to llvm::raw_ostream::write(char const*, unsigned long)’
aklo-aklo.o: In function print(long)': /mnt/a2/aklo.cpp:398: undefined reference to llvm::raw_ostream::operator<<(long)’
aklo-aklo.o: In function llvm::raw_ostream::operator<<(char)': /home/a/llvm/include/llvm/Support/raw_ostream.h:141: undefined reference to llvm::raw_ostream::write(unsigned char)’
aklo-aklo.o: In function llvm::cl::list<std::string, bool, llvm::cl::parser<std::string> >::getOptionWidth() const': /home/a/llvm/include/llvm/Support/CommandLine.h:1387: undefined reference to llvm::cl::basic_parser_impl::getOptionWidth(llvm::cl::Option const&) const’
aklo-aklo.o: In function llvm::cl::list<std::string, bool, llvm::cl::parser<std::string> >::printOptionInfo(unsigned long) const': /home/a/llvm/include/llvm/Support/CommandLine.h:1390: undefined reference to llvm::cl::basic_parser_impl::printOptionInfo(llvm::cl::Option const&, unsigned long) const’
aklo-aklo.o: In function ~basic_parser': /home/a/llvm/include/llvm/Support/CommandLine.h:763: undefined reference to vtable for llvm::cl::basic_parserstd::string
aklo-aklo.o: In function ~Option': /home/a/llvm/include/llvm/Support/CommandLine.h:290: undefined reference to vtable for llvm::cl::Option’
aklo-aklo.o: In function ~basic_parser': /home/a/llvm/include/llvm/Support/CommandLine.h:763: undefined reference to vtable for llvm::cl::basic_parserstd::string
aklo-aklo.o: In function ~Option': /home/a/llvm/include/llvm/Support/CommandLine.h:290: undefined reference to vtable for llvm::cl::Option’
aklo-aklo.o: In function llvm::raw_ostream::operator<<(llvm::StringRef)': /home/a/llvm/include/llvm/Support/raw_ostream.h:166: undefined reference to llvm::raw_ostream::write(char const*, unsigned long)’
aklo-aklo.o: In function main': /mnt/a2/aklo.cpp:434: undefined reference to llvm::sys::PrintStackTraceOnErrorSignal(bool)’
aklo-aklo.o: In function PrettyStackTraceProgram': /home/a/llvm/include/llvm/Support/PrettyStackTrace.h:63: undefined reference to llvm::PrettyStackTraceEntry::PrettyStackTraceEntry()’
/home/a/llvm/include/llvm/Support/PrettyStackTrace.h:63: undefined reference to vtable for llvm::PrettyStackTraceProgram' /home/a/llvm/include/llvm/Support/PrettyStackTrace.h:64: undefined reference to llvm::EnablePrettyStackTrace()’
aklo-aklo.o: In function main': /mnt/a2/aklo.cpp:456: undefined reference to llvm::cl::AddExtraVersionPrinter(void (*)())’
/mnt/a2/aklo.cpp:457: undefined reference to llvm::cl::ParseCommandLineOptions(int, char const* const*, char const*)' /mnt/a2/aklo.cpp:460: undefined reference to llvm::MemoryBuffer::getFileOrSTDIN(llvm::Twine const&, long, bool)’
/mnt/a2/aklo.cpp:469: undefined reference to llvm::outs()' aklo-aklo.o: In function ~PrettyStackTraceProgram’:
/home/a/llvm/include/llvm/Support/PrettyStackTrace.h:58: undefined reference to vtable for llvm::PrettyStackTraceProgram' /home/a/llvm/include/llvm/Support/PrettyStackTrace.h:58: undefined reference to llvm::PrettyStackTraceEntry::~PrettyStackTraceEntry()’
aklo-aklo.o: In function llvm::raw_ostream::operator<<(char)': /home/a/llvm/include/llvm/Support/raw_ostream.h:141: undefined reference to llvm::raw_ostream::write(unsigned char)’
aklo-aklo.o: In function main': /mnt/a2/aklo.cpp:463: undefined reference to llvm::errs()’
/mnt/a2/aklo.cpp:463: undefined reference to llvm::SMDiagnostic::print(char const*, llvm::raw_ostream&, bool, bool) const' aklo-aklo.o: In function PrettyStackTraceProgram’:
/home/a/llvm/include/llvm/Support/PrettyStackTrace.h:63: undefined reference to llvm::PrettyStackTraceEntry::~PrettyStackTraceEntry()' aklo-aklo.o: In function ~PrettyStackTraceProgram’:
/home/a/llvm/include/llvm/Support/PrettyStackTrace.h:58: undefined reference to vtable for llvm::PrettyStackTraceProgram' /home/a/llvm/include/llvm/Support/PrettyStackTrace.h:58: undefined reference to llvm::PrettyStackTraceEntry::~PrettyStackTraceEntry()’
aklo-aklo.o: In function __static_initialization_and_destruction_0': /mnt/a2/aklo.cpp:164: undefined reference to llvm::SourceMgr::~SourceMgr()’
aklo-aklo.o: In function Option': /home/a/llvm/include/llvm/Support/CommandLine.h:253: undefined reference to vtable for llvm::cl::Option’
/home/a/llvm/include/llvm/Support/CommandLine.h:253: undefined reference to llvm::cl::GeneralCategory' aklo-aklo.o: In function parser’:
/home/a/llvm/include/llvm/Support/CommandLine.h:938: undefined reference to vtable for llvm::cl::parser<std::string>' aklo-aklo.o: In function llvm::cl::list<std::string, bool, llvm::cl::parserstd::string >::done()’:
/home/a/llvm/include/llvm/Support/CommandLine.h:1398: undefined reference to llvm::cl::Option::addArgument()' aklo-aklo.o: In function ~basic_parser’:
/home/a/llvm/include/llvm/Support/CommandLine.h:763: undefined reference to vtable for llvm::cl::basic_parser<std::string>' aklo-aklo.o: In function ~Option’:
/home/a/llvm/include/llvm/Support/CommandLine.h:290: undefined reference to vtable for llvm::cl::Option' aklo-aklo.o:(.rodata._ZTIN4llvm2cl4listISsbNS0_6parserISsEEEE[_ZTIN4llvm2cl4listISsbNS0_6parserISsEEEE]+0x18): undefined reference to typeinfo for llvm::cl::Option’
aklo-aklo.o:(.rodata._ZTVN4llvm2cl4listISsbNS0_6parserISsEEEE[_ZTVN4llvm2cl4listISsbNS0_6parserISsEEEE]+0x20): undefined reference to llvm::cl::Option::anchor()' aklo-aklo.o:(.rodata._ZTVN4llvm2cl4listISsbNS0_6parserISsEEEE[_ZTVN4llvm2cl4listISsbNS0_6parserISsEEEE]+0x48): undefined reference to llvm::cl::Option::addOccurrence(unsigned int, llvm::StringRef, llvm::StringRef, bool)’
collect2: error: ld returned 1 exit status

Hi Russel,

Can you try compiling aklo.o using -fno-rtti prior to linking?

Tried that just now, same result.

In my experience I resolved undefined reference to vtable errors by
enabling RTTI during the LLVM build (CMake option :
-DLLVM_ENABLE_RTTI=ON).

Since the log tells you that llvm::outs() is not found, and since it is supposed to be found in libLLVMSupport.a, you can try:

$ nm /home/a/build//lib/libLLVMSupport.a | grep ZN4llvm4outsEv

0000000000002070 T __ZN4llvm4outsEv

The output should have the symbol defined.

Yeah, seems to.

a@a-VirtualBox:~$ nm /home/a/build//lib/libLLVMSupport.a | grep ZN4llvm4outsEv
U _ZN4llvm4outsEv
U _ZN4llvm4outsEv
0000000000000000 b _ZGVZN4llvm4outsEvE1S
0000000000000000 T ZN4llvm4outsEv
0000000000000000 r ZZN4llvm4outsEvE19__PRETTY_FUNCTION

0000000000000000 b _ZZN4llvm4outsEvE1S
U _ZN4llvm4outsEv

I can try building with RTTI, it’ll just take a few hours to run the build.

Looking again at your link line, I think static library has to appear after their uses. So try to put the .o before the list of library.

Right, it needed to be in LDADD instead of LDFLAGS. The combination of that and -fno-rtti did the trick. Thanks!