www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - shared objects

reply rochus <rochus rochus.net> writes:
Hi,
Is it possible to write shared objects for linux with DMD? If "yes", how
would one do that - and if "no", when will DMD be able to compile to
shared objects?

BG
Nicolai Waniek
Jan 08 2007
parent reply "Christian Kamm" <kamm nospam.de> writes:
 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
Jan 08 2007
parent reply rochus <rochus rochus.net> writes:
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
Jan 08 2007
next sibling parent reply Witold Baryluk <baryluk mpi.int.pl> writes:
On Mon, 08 Jan 2007 21:18:32 +0100rochus <rochus rochus.net> wrote:

 Christian Kamm wrote:
 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)
 ...
You must run /sbin/ldconfig, to update ld caches. -- Witold Baryluk MAIL: baryluk smp.if.uj.edu.pl, baryluk mpi.int.pl JID: movax jabber.autocom.pl
Jan 08 2007
parent rochus <rochus rochus.net> writes:
Witold Baryluk wrote:
 On Mon, 08 Jan 2007 21:18:32 +0100rochus <rochus rochus.net> wrote:
 
 
 You must run /sbin/ldconfig, to update ld caches.
 
Sorry but that's not true. As long as the path (in this case, /usr/local/lib) is somewhere noted within ld.so.conf this path will be searched when a user wants to link against a library. Nevertheless, I gave it a try and called ldconfig - it didn't work. best regards, Nicolai
Jan 08 2007
prev sibling parent reply "Christian Kamm" <kamm nospam.de> writes:
 I gave it a try but I didn't succeed, maybe you could help me again:
Two things: First, remove the 'export'. For me, it doesn't work with it. Second, you defined a square.squareIt in your libsquare.so, but try to access it as libsquare.squareIt. Either put a 'module libsquare;' statement in square.d or use D's interface files: dmd -H should create such a file from your square.d. Then the compiler will use it when compiling with dmd app.d -L-lsquare. Cheers, Christian
Jan 08 2007
parent rochus <rochus rochus.net> writes:
Christian Kamm wrote:
 I gave it a try but I didn't succeed, maybe you could help me again:
Two things: First, remove the 'export'. For me, it doesn't work with it. Second, you defined a square.squareIt in your libsquare.so, but try to access it as libsquare.squareIt. Either put a 'module libsquare;' statement in square.d or use D's interface files: dmd -H should create such a file from your square.d. Then the compiler will use it when compiling with dmd app.d -L-lsquare. Cheers, Christian
Hi Christian and: Thank you! After omitting "export" and renaming everything to "libsquare.d", it worked. cheers! Nicolai
Jan 08 2007