How to create a shared library with DMD on Linux?

rochus rochus at rochus.net
Wed Jan 10 10:56:38 PST 2007


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: libsquare.tar.gz
Type: application/x-gzip
Size: 783 bytes
Desc: not available
Url : http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20070110/357aa0d9/attachment.bin 


More information about the Digitalmars-d-learn mailing list