[LLVMdev] How to call C++ code from LLVM

pablogreen pawelc6 at op.pl
Sat Jun 20 07:17:50 PDT 2009


Sorry could you help me? I have problem with using LLVM and some code in C
(.so file)
I want to use (simply example):

mylib.c:

#include <stdio.h>
void printString(char *str) {
    printf("%s", str);
}

mylib.h:

#ifndef MY_LIB_HEADER
#define MY_LIB_HEADER
void printString(char* X);
#endif
///////////////////////////////////////

Normally in C++ I do this: 
g++ -fPIC -c mylib.c          
g++ -shared -o libmylib.so mylib.o

(and after that :
 g++ -Wall -g -o prog.exe prog.c -I./ -lmylib -L./
 LD_LIBRARY_PATH=/home/mylinux/Desktop/test ./prog.exe)

My program I compile like this:
$ llvm-as -f prog.ll
$ llc -march=x86-64 prog.bc -f

but I have error after:
$ g++ -o prog prog.s -I./ -L./ -lmylib

/tmp/ccLAh0mg.o: In function `main':
asd.bc:(.text+0xd): undefined reference to `printString'
collect2: ld returned 1 exit status

in .bc file i have something like that:

; ModuleID = 'xyz'
internal constant [5 x i8] c"ello\00"		; <[5 x i8]*>:0 [#uses=1]

declare void @printInt(i32)

define i32 @main() {
entry:
	call void @printString(i8* getelementptr ([5 x i8]* @0, i32 0, i32 0))
	%tmpvar = alloca i32		; <i32*> [#uses=2]
	store i32 0, i32* %tmpvar
	%tmpvar1 = load i32* %tmpvar		; <i32> [#uses=1]
	ret i32 %tmpvar1
}
/////////////////////////////

Second possibility is try lli :working:
but when I try to:
LD_LIBRARY_PATH=/home/mylinux/Desktop/test lli prog.bc
i have:

ERROR: Program used external function 'printString' which could not be
resolved!
0   lli       0x00000000009efa43
1   libc.so.6 0x00007ff0f2ed5040
2   libc.so.6 0x00007ff0f2ed4fb5 gsignal + 53
3   libc.so.6 0x00007ff0f2ed6bc3 abort + 387
4   lli       0x000000000065f5c5
5   lli       0x000000000065fa6a
llvm::JIT::getPointerToFunction(llvm::Function*) + 362
6   lli       0x0000000000667248
7   lli       0x0000000000667474
8   lli       0x000000000066885d
9   lli       0x00000000004d8e4d
10  lli       0x000000000099fc3d
llvm::FPPassManager::runOnFunction(llvm::Function&) + 429
11  lli       0x00000000009a0016
llvm::FunctionPassManagerImpl::run(llvm::Function&) + 134
12  lli       0x00000000009a01cb
llvm::FunctionPassManager::run(llvm::Function&) + 75
13  lli       0x000000000065f6d8
llvm::JIT::runJITOnFunction(llvm::Function*) + 56
14  lli       0x000000000065fa19
llvm::JIT::getPointerToFunction(llvm::Function*) + 281
15  lli       0x00000000006609f4 llvm::JIT::runFunction(llvm::Function*,
std::vector<llvm::GenericValue, std::allocator<llvm::GenericValue> > const&)
+ 84
16  lli       0x000000000065c8aa
llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*,
std::vector<std::string, std::allocator<std::string> > const&, char const*
const*) + 1370
17  lli       0x00000000004d4d8f main + 671
18  libc.so.6 0x00007ff0f2ec05a6 __libc_start_main + 230
19  lli       0x00000000004d4399
Aborted
-- 
View this message in context: http://www.nabble.com/How-to-call-C%2B%2B-code-from-LLVM-tp24103450p24124138.html
Sent from the LLVM - Dev mailing list archive at Nabble.com.




More information about the llvm-dev mailing list