How to call C++ code from LLVM


How can I call C++ libraries (LLVM & Qt for example) from a language that’s implemented on top of LLVM?


You can call them the same way a C++ file compiled with llvm-g++ would
call them. Essentially, it's complicated enough that you probably
don't want to do it for any interface of non-trivial size; I would
suggest compiling C++ snippets to bitcode with llvm-g++ and linking
them into your program.

For LLVM in particular, though, you can use the C bindings, which are
much more straightforward to use for this sort of thing.


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


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


void printString(char* X);

Normally in C++ I do this:
g++ -fPIC -c mylib.c
g++ -shared -o 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() {
  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
0 lli 0x00000000009efa43
1 0x00007ff0f2ed5040
2 0x00007ff0f2ed4fb5 gsignal + 53
3 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
std::vector<std::string, std::allocator<std::string> > const&, char const*
const*) + 1370
17 lli 0x00000000004d4d8f main + 671
18 0x00007ff0f2ec05a6 __libc_start_main + 230
19 lli 0x00000000004d4399

Try compiling with GCC in C mode because C++ mangles the names
(e.g. to support overloading).

Thanks :jumping: