Using LLVM components

Dear all,

I’m a beginner in LLVM field. If any rudeness, I feel sorry to that.

I have checked-out the source and built successfully.
Now I want to use it, so I write a simple code.

// context.cpp

#include “llvm/LLVMContext.h”
int main() {
llvm::LLVMContext& context = llvm::getGlobalContext();
return 0;

$ clang++ llvm-config --cxxflags --ldflags --libs context.cpp

But it yells

/tmp/cc-maq8ar.o: In function main': /nfs/m1/99/r99922105/context.cpp:4: undefined reference to llvm::getGlobalContext()’
collect2: ld returned 1 exit status
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

Seems like it cannot find the library.
I have tried debugging

$ echo llvm-config --cxxflags --ldflags --libs

-I/home/master/99/r99922105/llvm-101011/include -I/home/master/99/r99922105/llvm-101011/include -D_DEBUG -D_GNU_SOURCE -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -g -fno-exceptions -fno-rtti -fPIC -Woverloaded-virtual -Wcast-qual -L/nfs/m1/99/r99922105/llvm-101011/Debug+Asserts/lib -lpthread -ldl -lm -lLLVMMCDisassembler -lLLVMXCoreAsmPrinter -lLLVMXCoreCodeGen -lLLVMXCoreInfo -lLLVMSystemZAsmPrinter -lLLVMSystemZCodeGen -lLLVMSystemZInfo -lLLVMSparcAsmPrinter -lLLVMSparcCodeGen -lLLVMSparcInfo -lLLVMPowerPCAsmPrinter -lLLVMPowerPCCodeGen -lLLVMPowerPCInfo -lLLVMPTXAsmPrinter -lLLVMPTXCodeGen -lLLVMPTXInfo -lLLVMMipsAsmPrinter -lLLVMMipsCodeGen -lLLVMMipsInfo -lLLVMMSP430CodeGen -lLLVMMSP430Info -lLLVMMSP430AsmPrinter -lLLVMMBlazeAsmPrinter -lLLVMMBlazeCodeGen -lLLVMMBlazeInfo -lLLVMLinker -lLLVMipo -lLLVMInterpreter -lLLVMInstrumentation -lLLVMJIT -lLLVMExecutionEngine -lLLVMCppBackend -lLLVMCppBackendInfo -lLLVMCellSPUAsmPrinter -lLLVMCellSPUCodeGen -lLLVMCellSPUInfo -lLLVMCBackend -lLLVMCBackendInfo -lLLVMBlackfinAsmPrinter -lLLVMBlackfinCodeGen -lLLVMBlackfinInfo -lLLVMBitWriter -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86AsmPrinter -lLLVMX86Info -lLLVMAsmParser -lLLVMARMDisassembler -lLLVMARMAsmParser -lLLVMARMCodeGen -lLLVMARMAsmPrinter -lLLVMARMInfo -lLLVMArchive -lLLVMBitReader -lLLVMAlphaAsmPrinter -lLLVMAsmPrinter -lLLVMMCParser -lLLVMAlphaCodeGen -lLLVMSelectionDAG -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMMC -lLLVMCore -lLLVMAlphaInfo -lLLVMSupport -lLLVMSystem

It seems everything is OK.

I have no idea what I should fix now.
Could anyone give me some advices? I appreciate for that.

Thank you.

"汶翰 (Wen-Han)" <> writes:


$ clang++ `llvm-config --cxxflags --ldflags --libs` context.cpp

Replace that with

clang++ context.cpp `llvm-config --cxxflags --ldflags --libs`

Libraries must be listed after the source or object files.