[LLVMdev] (Newbie) Using lli with clang++?

315 views
Skip to first unread message

Christopher Jones

unread,
Mar 1, 2012, 10:24:09 PM3/1/12
to llv...@cs.uiuc.edu
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<iostream>
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

陳韋任

unread,
Mar 1, 2012, 10:50:48 PM3/1/12
to Christopher Jones, llv...@cs.uiuc.edu
> $ 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

--
Wei-Ren Chen (陳韋任)
Computer Systems Lab, Institute of Information Science,
Academia Sinica, Taiwan (R.O.C.)
Tel:886-2-2788-3799 #1667
Homepage: http://people.cs.nctu.edu.tw/~chenwj

_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Christopher Jones

unread,
Mar 2, 2012, 12:38:44 AM3/2/12
to 陳韋任, llv...@cs.uiuc.edu
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

Christopher Jones

unread,
Mar 2, 2012, 1:06:21 AM3/2/12
to 陳韋任, llv...@cs.uiuc.edu
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

On Mar 1, 2012, at 10:50 PM, 陳韋任 wrote:

Matt Johnson

unread,
Mar 2, 2012, 1:14:08 AM3/2/12
to Christopher Jones, llv...@cs.uiuc.edu


On 03/01/2012 09:24 PM, Christopher Jones wrote:
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<iostream>
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

$ 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


Duncan Sands

unread,
Mar 2, 2012, 2:13:46 AM3/2/12
to llv...@cs.uiuc.edu
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.

Reply all
Reply to author
Forward
0 new messages