(Newbie) Using lli with clang++?

Hello all,

I’m brand new to using LLVM and am having trouble using lli with a C++ program. I tried to compile the following:

#include
using namespace std;
int main()
{
cout << “Hello, world!” << endl;
return 0;
}

When I compile directly to an executable with the following command, all is well:
$ clang++ -O3 hello.cpp -o hello

But when I try to produce a bitcode file, I get an error:

$ clang++ -O3 -emit-llvm hello.cpp -c -o hello.bc
$ lli hello.bc
LLVM ERROR: Program used external function ‘_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l’ which could not be resolved!

I’m running this on x86_64. I’d appreciate any help about what I’m doing wrong.
Thanks!

Chris

$ clang++ -O3 -emit-llvm hello.cpp -c -o hello.bc
$ lli hello.bc
LLVM ERROR: Program used external function
'_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l'
which could not be resolved!

  What version of LLVM and Clang you are using? I have no such problem
on my machine.

Regards,
chenwj

I'm using 3.1 for both Clang and LLVM:

$ lli -version
LLVM version 3.1svn
  DEBUG build with assertions.
  Built Feb 29 2012 (17:54:38).
  Default target: x86_64-unknown-linux-gnu

$ clang -v
clang version 3.1 (3edf02f66d339a3ae6d06aeb96c78d9089b53bc1)
Target: x86_64-unknown-linux-gnu
Thread model: posix

Thanks,
Chris

Something else that may help: when I try to use llc to generate native assembly, I see that I have a linking problem:

$ llc hello.bc -o hello.s
$ g++ hello.s -o hello.native
In function `main':
hello.bc:(.text+0x11): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)'
hello.bc:(.text+0x3b): undefined reference to `std::ctype<char>::_M_widen_init() const'
collect2: ld returned 1 exit status

What should I do to modify the following line to link to the standard library?
$ clang++ -O3 -emit-llvm hello.cpp -c -o hello.bc

I'll also mention that when I try this exercise using a C program and clang instead of clang++, lli works fine.

Thanks,
Chris

Hello all,

I’m brand new to using LLVM and am having trouble using lli with a C++ program. I tried to compile the following:

#include
using namespace std;
int main()
{
cout << “Hello, world!” << endl;
return 0;
}

When I compile directly to an executable with the following command, all is well:
$ clang++ -O3 hello.cpp -o hello

But when I try to produce a bitcode file, I get an error:

$ clang++ -O3 -emit-llvm hello.cpp -c -o hello.bc

hello.bc doesn’t contain the libstdc++ bits your program needs (iostream and its (many) dependencies). When you produce an executable, clang tells the linker to link your binary with libsupc++, libstdc++, and others, so the dynamic linker can satisfy your iostream dependencies at runtime. When running under lli, the interpreter will provide a few basic functions for you (see lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp), but things like exit(), abort(), printf(), and scanf(), nothing as complicated as libstdc++. So if the function you need is not in the short list provided by the interpreter itself, it will try to find your function using libffi (if you compiled it in). If that doesn’t work, you’ll get errors like the below.

One solution would be to try to generate a single big .bc file that is “statically linked” with all your dependencies (for some clues as to what these are, try “ldd ./hello” on your clang+±generated binary. Unfortunately, I’m no expert on this or any other methods of informing lli about your .bc file’s dependencies and where they can be found when your interpreted program calls out to them.

-Matt

Hi,

...

When I compile directly to an executable with the following command, all is well:
$ clang++ -O3 hello.cpp -o hello

But when I try to produce a bitcode file, I get an error:

$ clang++ -O3 -emit-llvm hello.cpp -c -o hello.bc
$ lli hello.bc
LLVM ERROR: Program used external function
'_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l'
which could not be resolved!

I'm running this on x86_64. I'd appreciate any help about what I'm doing wrong.

first off you need to build with FFI support (configure with --enable-libffi).
Then you doubtless need to pass libstdc++ to lli, like this (IIRC):
-load=libstdc++.so
When you compile with clang++ it automagically adds the C++ standard library
to the list of things to link with, which is why you don't notice that the
linker is getting passed libstdc++.so. As lli is doing linking too, it also
needs libstdc++.so.

Ciao, Duncan.