dynamic library building and loading

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Sep 27 11:25:06 PDT 2012


On 9/27/12 6:26 AM, Maxim Fomin wrote:
[snip]

Thanks! I adapted your code as follows and it works with 2.058 on Centos.

*** lib.d
import std.stdio;

extern(C) int fun()
{
     writeln(", world!");
     return 42;
}

*** main.d
import std.stdio;

extern(C) int fun();

void main()
{
     write("Hello");
     fun();
}

*** Makefile

PHOBOS_PATH=/path/to/phobos/

main: main.o liblib.so
	gcc main.o -llib -lphobos2 -lpthread -lrt -L. -L${PHOBOS_PATH} 
-Wl,-rpath=. -o main

liblib.so: lib.d
	dmd -fPIC -c lib.d
	gcc -shared lib.o -o liblib.so

%.o: %.d
	dmd -c $<

clean:
	rm -f main main.o lib.o liblib.so

Building the makefile and running ./main prints the classic message. 
Thanks all for answering, this will get us going.


Andrei


More information about the Digitalmars-d mailing list