How to create a shared library with DMD on Linux?

Bradley Smith digitalmars-com at baysmith.com
Wed Jan 10 13:35:20 PST 2007


Yes, your example works, but when I start using the standard library 
things don't work.

Suppose I want to create a debugging version of a library with simple 
print statement.

     int squareIt(int i)
     {
	writefln("squareIt ", i);
	return i*i;
     }

The app_d will now crash, and the app_c will not link because 
std.stdio.writefln is not defined. For app_c, if I add -lphobos -lm 
-lpthread, it still has unresolved references in deh2.o.

I also tried to add phobos/internal/deh2.d to the libsquare.so, but that 
  gives an unresolved symbol _d_throw at 4.

Any other ideas?

Thanks,
   Bradley


rochus wrote:
> Bradley Smith wrote:
>> I was hoping that I was doing something wrong and it could be linked
>> against a non-D language.
>>
>> Thanks,
>>   Bradley
> 
> Hi Bradly,
> 
> Good news! The topic won't let me sleep and i figured out how to create
> a shared object that may be accessed from an application written in
> plain C, so i guess it might work for your JNI-problem, too.
> 
> The "magic" thing to add is:
> Instead of simply "exporting" your functions in your library, place them
> within an "extern (C)"-Block:
> 
> extern (C)
> {
>     int myFunction(int i)
>     {
>         doSomething;
>         return somethingElse;
>     }
> }
> 
> I attached my sample layout, a library that has one function called
> "squareIt" (guess what it does *g*). There are three directories: app_c,
> app_d and lib, each containing a Makefile. They should be self
> explaining as there's not much in them. Important though: i called dmd
> with the argument "-fPIC" when compiling the library, though I don't
> think that this switch is recognized - it's just there to remind myself
> that it's PIC we're gonna create.
> 
> So here's what you should do:
> 
> compile the library:
> #cd lib
> #make
> 
> copy the library to a place, where ld might find it. for example
> /usr/lib or /usr/local/lib or temporarily alter the LD_LIBRARY_PATH var
> to /where/you/extracted/the/sample/lib/bin
> 
> compile the sample applications:
> #cd app_d
> #make
> 
> #cd app_c
> #make
> 
> Run the applications. They worked for me.
> 
> good luck,
> Nicolai


More information about the Digitalmars-d-learn mailing list