Thread overview
Using D as a shared library.
May 14, 2013
Paolo & Kevin
May 14, 2013
Jacob Carlborg
May 15, 2013
evilrat
May 14, 2013
I am trying to use a D shared library with a C program.
But I have a problem, the code works in a D program.
But it crashes as a library.

Here is a minimal example.
I want to make a library that returns an array of 100 integers.
% ls
fakemain.d  function.d  main.c
% cat fakemain.d
void main() {}
% cat function.d
extern (C) {
int* blah() {
    int[] a;
    a.length = 100;
    return a.ptr; } }
% cat main.c
int* blah();
int main() {
    int* p;
    p = blah();
    p[0] = 1;
    return p[0];}
%

Lets compile everything:
% dmd -fPIC -c fakemain.d
% dmd -fPIC -c function.d
% gcc -c main.c
% ls
fakemain.d  fakemain.o  function.d  function.o  main.c  main.o

Lets link, fakemain.o is needed to avoid linking problems:
% gcc -fPIC -shared -o libfunction.so function.o
% gcc -L"$PWD" main.o fakemain.o -lfunction -lphobos2
% ls
a.out*  fakemain.d  fakemain.o  function.d  function.o  libfunction.so*  main.c  main.o

Lets try...
% LD_LIBRARY_PATH="$PWD" ./a.out
[1]    31468 segmentation fault (core dumped)  LD_LIBRARY_PATH="$PWD" ./a.out
% LD_LIBRARY_PATH="$PWD" gdb -q ./a.out
Reading symbols from /home/paolo/uni/ostar/ostar/c_exact_algorithm/tmp/a.out...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/paolo/uni/ostar/ostar/c_exact_algorithm/tmp/a.out
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?

Program received signal SIGSEGV, Segmentation fault.
0x00000000004037d1 in gc_qalloc ()
(gdb) bt
#0  0x00000000004037d1 in gc_qalloc ()
#1  0x000000000040199c in _d_arraysetlengthT ()
#2  0x00007ffff7bda842 in blah () from /home/paolo/uni/ostar/ostar/c_exact_algorithm/tmp/libfunction.so
#3  0x0000000000400ad2 in main ()
(gdb)
It fails :(

What I am doing wrong?
I think there is something the D language does before starting main.
And the C language does not. Can I initialize the program correctly
manually? Or it is another problem altogether?

Thanks,
Paolo & Kevin
May 14, 2013
On 2013-05-14 15:53, Paolo & Kevin wrote:
> I am trying to use a D shared library with a C program.
> But I have a problem, the code works in a D program.
> But it crashes as a library.
>

> What I am doing wrong?
> I think there is something the D language does before starting main.
> And the C language does not. Can I initialize the program correctly
> manually? Or it is another problem altogether?

The D runtime does quite a lot before running D main. You would need to a least initialize the runtime. Here's the C main function that is run before D main:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dmain2.d#L384

Here's the function to you can call to initialize the runtime from C:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dmain2.d#L281

And to terminate:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dmain2.d#L312

I'm not sure if all this work yet. Using a D shared library from a D application, "statically" linked, should work on Linux 64bit. Don't know if it works from a C application.

-- 
/Jacob Carlborg
May 15, 2013
On Tuesday, 14 May 2013 at 16:01:57 UTC, Jacob Carlborg wrote:
> ...
> I'm not sure if all this work yet. Using a D shared library from a D application, "statically" linked, should work on Linux 64bit. Don't know if it works from a C application.

loading D libraries on non-Windows possible but all libs would have its own GC(with all cons...), only on Windows loaded lib attaches its GC to "main"(ur app or you can manually attach them to another other lib GC) GC.

i hope it be helpful.