Dynamic D Library

Daniel Keep daniel.keep.lists at gmail.com
Tue Jul 28 00:02:31 PDT 2009



teo wrote:
> 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
> 
>> ...
> 
> split your files in two directories and compile them separately

So your justification for it not working is because you deliberately
didn't follow his instructions and broke the compile?

> 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'

Well of course it can't compile: YOU MOVED test.d!  What do you expect
when you tell it to import a file that no longer exists?  The compiler
is not psychic.

If you really, absolutely have to have the two parts in different
directories, just generate a header file.

dmd -H -c -o- test.d

That generates test.di, which you have to make available to prog.d.

Of course, in this particular case, test.di is exactly the same as
test.d, since all the functions are small enough to be inlined.



More information about the Digitalmars-d mailing list