C API linking problem

Hello all,

Does anyone have any inside why I can't get the below simple C API test to
link? This is on a 32-bit Gentoo Linux system and LLVM TOT which was
compiled with enable-optimized, gcc is 4.3.2.

15:26|melis@juggle2:~/c/llvmpy> cat t.c
#include "llvm-c/Core.h"

int main()
{
    LLVMContextRef ctx;
    ctx = LLVMContextCreate();
    return 0;
}

15:29|melis@juggle2:~/c/llvmpy> llvm-config --cflags --ldflags --libs all
-I/home/melis/llvm/include -D_DEBUG -D_GNU_SOURCE -D__STDC_LIMIT_MACROS
-D__STDC_CONSTANT_MACROS -O3 -fomit-frame-pointer -fPIC
-L/home/melis/llvm/lib -lpthread -ldl -lm
-lLLVMLinker -lLLVMipo -lLLVMInterpreter -lLLVMInstrumentation -lLLVMJIT
-lLLVMExecutionEngine -lLLVMDebugger -lLLVMCBackend -lLLVMCBackendInfo
-lLLVMBitWriter -lLLVMX86AsmParser -lLLVMX86AsmPrinter -lLLVMX86CodeGen
-lLLVMSelectionDAG -lLLVMX86Info -lLLVMAsmPrinter -lLLVMCodeGen
-lLLVMScalarOpts -lLLVMTransformUtils -lLLVMipa -lLLVMAsmParser
-lLLVMArchive -lLLVMBitReader -lLLVMAnalysis -lLLVMTarget -lLLVMMC
-lLLVMCore -lLLVMSupport -lLLVMSystem

15:29|melis@juggle2:~/c/llvmpy> gcc -W -Wall -o blah `llvm-config --cflags
--ldflags --libs all` t.c
/tmp/ccs4MbKp.o: In function `main':
t.c:(.text+0x21): undefined reference to `LLVMContextCreate'
collect2: ld returned 1 exit status

15:29|melis@juggle2:~/c/llvmpy> nm ~/llvm/lib/libLLVMCore.a | grep
LLVMContextCreate
00001bc0 T LLVMContextCreate

Regards,
Paul

"Paul Melis" <llvm@assumetheposition.nl> writes:

15:29|melis@juggle2:~/c/llvmpy> gcc -W -Wall -o blah `llvm-config --cflags
--ldflags --libs all` t.c
/tmp/ccs4MbKp.o: In function `main':
t.c:(.text+0x21): undefined reference to `LLVMContextCreate'
collect2: ld returned 1 exit status

15:29|melis@juggle2:~/c/llvmpy> nm ~/llvm/lib/libLLVMCore.a | grep
LLVMContextCreate
00001bc0 T LLVMContextCreate

Try this:

gcc t.c -W -Wall -o blah `llvm-config --cflags --libs all --ldflags`

Óscar Fuentes wrote:

"Paul Melis" <llvm@assumetheposition.nl> writes:

15:29|melis@juggle2:~/c/llvmpy> gcc -W -Wall -o blah `llvm-config
--cflags
--ldflags --libs all` t.c
/tmp/ccs4MbKp.o: In function `main':
t.c:(.text+0x21): undefined reference to `LLVMContextCreate'
collect2: ld returned 1 exit status

15:29|melis@juggle2:~/c/llvmpy> nm ~/llvm/lib/libLLVMCore.a | grep
LLVMContextCreate
00001bc0 T LLVMContextCreate

Try this:

gcc t.c -W -Wall -o blah `llvm-config --cflags --libs all --ldflags`

Thanks! Moving t.c to the front of the command-line fixes it (although
--ldflags still needs to go before --libs in the above line). And I need
to link with g++ instead of gcc.

I guess I didn't figure this one out because when you normally manually
specify libraries they always go *after* the source file argument.
Splitting the llvm-config call into a cxxflags and a --libs/--ldflags part
and putting the sources in between is probably cleaner in this respect.

Paul

"Paul Melis" <llvm@assumetheposition.nl> writes:

15:29|melis@juggle2:~/c/llvmpy> gcc -W -Wall -o blah `llvm-config
--cflags
--ldflags --libs all` t.c
/tmp/ccs4MbKp.o: In function `main':
t.c:(.text+0x21): undefined reference to `LLVMContextCreate'
collect2: ld returned 1 exit status

15:29|melis@juggle2:~/c/llvmpy> nm ~/llvm/lib/libLLVMCore.a | grep
LLVMContextCreate
00001bc0 T LLVMContextCreate

Try this:

gcc t.c -W -Wall -o blah `llvm-config --cflags --libs all --ldflags`

Are you sure that's what you meant? Now the linker can't find anything
anymore and I get tons of unresolved symbols.

That is because -L comes after the LLVM libraries.

Try

gcc t.c -W -Wall -o blah `llvm-config --cflags --ldflags --libs all`

However, if your linker is sensitive to library order, you may get
unresolved externals for -lpthread and other system libraries that
are list before the LLVM libraries on the llvm-config output.

If that happens try

gcc t.c -W -Wall -o blah -L/home/melis/llvm/lib `llvm-config --cflags --libs all --ldflags`

HTH.