Lots of linker errors when trying to link with the LLVM C++ API

Hello LLVM Developers,

I am trying to compile the program in llvm/examples/ModuleMaker/ModuleMaker.cpp.
I am executing the following command:

clang++ --std=c++14 llvm-config --cxxflags --ldflags --libs --libfiles --system-libs ~/src/llvm/examples/ModuleMaker/ModuleMaker.cpp

The linker produces the following errors:

/tmp/ModuleMaker-f710af.o: In function main': /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x17): undefined reference to llvm::LLVMContext::LLVMContext()’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x3b): undefined reference to llvm::Module::Module(llvm::StringRef, llvm::LLVMContext&)' /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x43): undefined reference to llvm::Type::getInt32Ty(llvm::LLVMContext&)’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x4d): undefined reference to llvm::FunctionType::get(llvm::Type*, bool)' /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x70): undefined reference to llvm::User::operator new(unsigned long)’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x88): undefined reference to llvm::Function::Function(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*)' /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0xc1): undefined reference to llvm::BasicBlock::BasicBlock(llvm::LLVMContext&, llvm::Twine const&, llvm::Function*, llvm::BasicBlock*)’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0xc9): undefined reference to llvm::Type::getInt32Ty(llvm::LLVMContext&)' /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0xd8): undefined reference to llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool)’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0xe3): undefined reference to llvm::Type::getInt32Ty(llvm::LLVMContext&)' /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0xf2): undefined reference to llvm::ConstantInt::get(llvm::IntegerType*, unsigned long, bool)’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x120): undefined reference to llvm::BinaryOperator::Create(llvm::Instruction::BinaryOps, llvm::Value*, llvm::Value*, llvm::Twine const&, llvm::Instruction*)' /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x133): undefined reference to llvm::SymbolTableListTraitsllvm::Instruction::addNodeToList(llvm::Instruction*)’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x160): undefined reference to llvm::User::operator new(unsigned long, unsigned int)' /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x173): undefined reference to llvm::ReturnInst::ReturnInst(llvm::LLVMContext&, llvm::Value*, llvm::Instruction*)’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x17e): undefined reference to llvm::SymbolTableListTraits<llvm::Instruction>::addNodeToList(llvm::Instruction*)' /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x19d): undefined reference to llvm::outs()’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x1af): undefined reference to llvm::WriteBitcodeToFile(llvm::Module const*, llvm::raw_ostream&, bool, llvm::ModuleSummaryIndex const*, bool)' /home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x1b7): undefined reference to llvm::Module::~Module()’
/home/nabeel/src/llvm/examples/ModuleMaker/ModuleMaker.cpp:(.text.main+0x1c7): undefined reference to llvm::LLVMContext::~LLVMContext()' /tmp/ModuleMaker-f710af.o:(.data._ZN4llvm30VerifyDisableABIBreakingChecksE+0x0): undefined reference to llvm::DisableABIBreakingChecks’
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)
Makefile:2: recipe for target ‘all’ failed
make: *** [all] Error 1

The output of clang -v:

clang version 4.0.0 (tags/RELEASE_400/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/nabeel/bin/clang/bin
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6.2.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.2.0
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64

From what I understand, there should be no linker errors because llvm-config produces the names of all the libraries that are distributed with LLVM.

Any help will be enormously appreciated.

You need your llvm-config <stuff> after the source file, so that it links in AFTER the source you are compiling - otherwise the linker will just skip over the whole library and say “well, nothing is using any of this, nothing is needed”.