Using the llvm D-bindings

Manuel König manuelk89 at gmx.net
Wed Oct 6 05:20:41 PDT 2010


Hi,

did anyone have success using the llvm-2.7 D-bindings from the bindings
project http://www.dsource.org/projects/bindings on x86_64 linux? I
tried so far with ldc, but the compiler chokes about a mere 3570 lines
of unresolved symbols when linking with ldc, compiling works fine.

Short version:
==============

I think the question boils down to "How do I create a D library and
link it into a c++ program on x86_64 linux using ldc (or gdc or
<any_other_D_compiler>)".

Long version:
=============
Here's a snip of the linker error messages I get from ldc:

// -- file main.d
import llvm.c.Core;

void main()
{
    auto context = LLVMGetGlobalContext();
}
// --

$ ldc -I=/path/to/llvm-2.7 \
-L=/usr/local/lib/libLLVM{Core,Support,System}.a main.d
/usr/local/lib/libLLVMCore.a(Core.o): In function `LLVMContextCreate':
Core.cpp:(.text+0x57): undefined reference to `operator new(unsigned
long)'
/usr/local/lib/libLLVMCore.a(Core.o): In function
`LLVMModuleCreateWithName':
Core.cpp:(.text+0x13a): undefined reference to `operator new(unsigned
long)'
/usr/local/lib/libLLVMCore.a(Core.o): In function
`LLVMModuleCreateWithNameInContext':
Core.cpp:(.text+0x195): undefined reference to `operator new(unsigned
long)'
/usr/local/lib/libLLVMCore.a(Core.o): In function `LLVMSetDataLayout':
Core.cpp:(.text+0x240): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> >::basic_string(char
const*, unsigned long, std::allocator<char> const&)'
Core.cpp:(.text+0x24f): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char>
>::assign(std::basic_string<char, std::char_traits<char>,
>std::allocator<char> > const&)'
Core.cpp:(.text+0x25e): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char>
>::_Rep::_S_empty_rep_storage'
Core.cpp:(.text+0x294): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char>
>::_Rep::_M_destroy(std::allocator<char> const&)'
> [...]

After scanning over the error messages, I think it's only operator,
delete and std::* symbols that are missing. I also get the same error
messages when I try to compile an equivalent C program that uses the
llvm-c/* headers with gcc, but it works with g++, so I'm quite sure the
problem is the missing c++ runtime, which is nonexistent in ldc.

I think there are only two ways to solve this:

(1) link the c++ runtime to ldc (I have no clue how to do that or if 
    it's possible)
(2) Write a D library using the llvm D-bindings, define a semi-main
    function as extern(C), and write a c++ wrapper program that links
    to the D library and does nothing more than calling the semi-main
    function. The llvm symbols and c++ runtime will then be linked in
    using g++.

Option (2) seems easy, but ldc does not seem to have a linker swith to
build libraries, and doing

ldc -c myprog.d # produces myprog.o
ar -cq myprog.a myprog.o

to generate a library from the object file doesn't work because there
are still undefined symbols to the main function or the module ctors
in there, even when myprog.d doesn't define a main function. I could
try to hack those missing functions back in by defining them in the C
program, but there must be some simpler way to generate libraries,
or am I at a loosing front here?

I also consider using other compilers than ldc as an option, but afaik
the only other usable compiler might be gdc (is that stable by now?).


More information about the Digitalmars-d-learn mailing list