2

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.

d11wtq
  • 34,788
  • 19
  • 120
  • 195

1 Answers1

3

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Ah, that is definitely different! I was hand-writing the build script in the other project and has my source files first. In the new project, they are just in `CXXFLAGS` so my Makefile is putting the source files after them. I'll restructure things a bit :) – d11wtq Mar 15 '14 at 11:29
  • Problem solved by simply moving `$(CXXFLAGS)` to be after the source files, thank you! Will accept in a few mins once the SO time restriction has passed. – d11wtq Mar 15 '14 at 11:32
  • 1
    I personally prefer to split the linker command into a `$CXXFLAGS` (Or raterh `$LDFLAG` for the linker side) and a `$LIBS`. – Mats Petersson Mar 15 '14 at 11:36