shared objects

rochus rochus at rochus.net
Mon Jan 8 12:18:32 PST 2007


Christian Kamm wrote:
>> Is it possible to write shared objects for linux with DMD?
> 
> Yes. I've only tried it with a single file, but it should work. Use dmd
> -c to compile without linking and then link manually with gcc
> <object-files> -shared <your flags> -o lib<name>.so.
> 
> I hope that helps,
> Christian

Hi Christian,

I gave it a try but I didn't succeed, maybe you could help me again:

First i created the file "square.d":
    export int squareIt(int i) {
        return i*i;
    }

I compiled and linked it with:
    dmd -c square.d
    gcc -shared -o libsquare.so libsquare.o

I copied the library to /usr/local/lib (where ld may find it)

Then i created another file just containing the function declaration
("libsquare.d"):
    export int squareIt(int i);

The next step was writing a little sample app ("app.d"):
    import std.stdio;
    // now the important line: import the lib:
    import libsquare;

    void main(char[][] args) {
        writefln("4*4 = %d", squareIt(4));
    }

I tried to compile it this way:
    dmd app.d libsquare.d -L-lsquare

But this gave me an error message:
    gcc app.o libsquare.o -o app -m32 -lphobos -lm -Xlinker -lsquare
    app.o: In function '_Dmain':
    app.d:(.gnu.linkonce.t_Dmain+0xa): undefined reference to
'_imp__D9libsquare8squareItFiZi'
    collect2: ld returned 1 exit status
    --- errorlevel 1

As I'm new to shared objects under linux, I don't know how to proceed
right now.


Nicolai



More information about the Digitalmars-d mailing list