Using llvm::Function::dump(), linker gives “undefi

2019-07-23 01:28发布

问题:

I'm trying to generate LLVM IR code, which I have done successfully as part of the Kaleidoscope tutorial on this same machine, using these same compiler flags.

My code compiles without errors in clang++ 3.4. However, at link time I'm getting:

undefined reference to `llvm::Value::dump() const'

The error is being triggered by the line:

if (generator.code()) // returns llvm::Function*, or NULL
  generator.code()->dump();

If I remove the call to dump(), the linker is happy.

Clang++ flags I'm using are:

-O3 -g -Wall -std=c++11 -I./src `llvm-config --cppflags --ldflags --libs core jit native`

I'm confused, because the Kaleidoscope project compiles and runs fine and uses the same compiler flags and is built on the same computer.

回答1:

When linking with libraries the libraries has to be placed after the source/object files.

So you need something like

clang++ -O3 -g -Wall -std=c++11 -I./src \
    `llvm-config --cppflags --ldflags core jit native` \
    objectfile1.o objectfile2.o \
    `llvm-config --libs core jit native` \
    -o outputfile

It's because the linker looks for symbols in the order they are given on the command line.



标签: c++ clang llvm