Dynamic D Library

teo teo.ubuntu at yahoo.com
Sun Jul 26 22:35:32 PDT 2009


On Fri, 17 Jul 2009 06:15:18 +0000, Jesse Phillips wrote:

> On Fri, 17 Jul 2009 01:35:37 -0400, teo wrote:
> 
>> Hmm, you say it is working. Can you show me how to do that on 32-bit
>> Linux with dmd? How would you compile Phobos as an external library?
>> Please let me know, if you are aware of an article describing that.
>> Thank you in advance.
> 
> My guess is that you should be able to follow a shared library tutorial
> 
> http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
> 
> Then just modify the GCC commands for DMD
> 
> gcc -Wall -fPIC -c *.c
> 
> Becomes:
> dmd -fPIC -c *.d
> 
> Probably can leave alone:
> gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0   *.o
> 
> gcc -Wall -L/opt/lib prog.c -lctest -o prog
> 
> Becomes:
> dmd -L-L/opt/lib prog.d -L-lctest
> 
> I have not tested this, but nor have I got it to work in C, but I only
> blame myself.

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 is my setup:
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/lib

==========
==========
Only this case WORKS!

module test; // file "test.d"
extern(C)
{
	int test1()
	{
		return 1;
	}
	string test2()
	{
		return "Hello World!";
	}
}
$ dmd -fPIC -c test.d 
$ gcc -shared -o libtest.so test.o
$ sudo mv libtest.so /opt/lib/
---
module main; // file "prog.d"
import std.stdio;
extern(C) int test1();
extern(C) string test2();
void main()
{
	writefln("Result: %d", test1());
	writefln("Result: %s", test2());
	return;
}
$ dmd prog.d -L-L/opt/lib -L-ltest

==========
DOESN'T WORK without extern(C). Cannot compile the program.

module test; // file "test.d"
int test()
{
	return 1;
}
$ dmd -fPIC -c test.d 
$ gcc -shared -o libtest.so test.o
$ sudo mv libtest.so /opt/lib/
---
module main; // file "prog.d"
import std.stdio;
extern(C) int test();
void main()
{
	writefln("Result: %d", test());
	return;
}
$ dmd prog.d -L-L/opt/lib -L-ltest

prog.o: In function `_Dmain':
prog.d:(.text._Dmain+0x10): undefined reference to `test'
collect2: ld returned 1 exit status
--- errorlevel 1

==========
DOESN'T WORK with classes defined in the library. The program cannot be 
compiled.

module test; // file "test.d"
extern(C)
{
	class Test
	{
		private int n;
		public this(int i)
		{
			n = i;
		}
	}
	Test getTest()
	{
		return new Test(1);
	}
}
$ dmd -fPIC -c test.d 
$ gcc -shared -o libtest.so test.o
$ sudo mv libtest.so /opt/lib/
---
module main; // file "prog.d"
extern(C) Test getTest();
void main()
{
	Test t = getTest();
	return;
}
$ dmd prog.d -L-L/opt/lib -L-ltest

prog.d(3): Error: identifier 'Test' is not defined
prog.d(3): Error: Test is used as a type

==========
DOESN'T WORK when Phobos is referenced within the library.

module test; // file "test.d"
import std.stdio;
extern(C)
{
	void test()
	{
		writefln("Hello World!");
		return;
	}
}
$ dmd -fPIC -c test.d 
$ gcc -shared -o libtest.so test.o

/usr/bin/ld: libtest.so: No symbol version section for versioned symbol 
`_d_throw at 4'
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status

==========



More information about the Digitalmars-d mailing list