Dynamic D Library

teo teo.ubuntu.remove at yahoo.com
Mon Jul 27 22:03:13 PDT 2009


On Mon, 27 Jul 2009 20:34:44 +0000, BCS wrote:

> Reply to teo,
> 
>> I did some tests and here are the results: D cannot be used in Shared
>> Objects. The only case that works is when no classes are exported and
>> when there are no references to Phobos within the library.
> 
> 
> this works:
> 
no it does not

> module test;
> int test1() { return 0; }
> char[] test2() { return "hello world"; }
> 
> class C
> {
>     int i;
>     this(int j) { i = j; }
>     int get(){ return i; }
> }
> 
> ----
> 
> module main;
> import test;
> void main()
> {
>    test1();
>    test2();
>    int i = (new C(5)).get();
> }
> 
> $ dmd -fPIC -c test.d
> $ gcc -shared -o libtest.so test.o
> $ dmd main.d -L-L`pwd` -L-ltest
> 
> All the problems were resolved by replacing the prototypes with imports.

split your files in two directories and compile them separately

in lib(rary) directory I have test.d

module test; // file "test.d"
int test1() { return 1; }
class Test
{
	private int n;
	public this(int i) { n = i; }
}
$ cd lib/
$ dmd -fPIC -c test.d 
$ gcc -shared -o libtest.so test.o
$ sudo mv libtest.so /opt/lib/

and prog.d in exe(cutable) directory

module main; // file "prog.d"
import std.stdio;
import test;
void main()
{
	writefln("Result: %d", test1());
	Test t = new Test(1);
	return;
}
$ cd ../exe/
$ dmd prog.d -L-L/opt/lib -L-ltest
prog.d(2): Error: module test cannot read file 'test.d'




More information about the Digitalmars-d mailing list