Cross-platform dynamic libraries (GNU/Linux, Mac OS X, Windows, ...)

Gregor Richards Richards at codu.org
Mon Sep 10 09:11:06 PDT 2007


Hugues De Keyzer wrote:
> Gregor Richards wrote:
>> Hugues De Keyzer wrote:
>>> Hi,
>>>
>>> I'm planning to create a multi-platform (at least GNU/Linux, Mac OS X 
>>> and Windows) application in D. One of its key characteristics is that 
>>> it will be based on modules (plug-ins). Ideally, functionalities more 
>>> advanced than just normal shared libraries would be desirable.
>>>
>>> I took a look at DDL, but it seems to be targeted only at Windows.
>>>
>>> I made some dlopen() tests on Mac OS X 10.4 with gdc. It works, as 
>>> long as the garbage collector isn't involved in the library. 
>>> Instancing a class in the library works only if new() and delete() 
>>> are defined and don't call the gc. When new() and delete() are not 
>>> defined, instancing a class results in a bus error. gdb gives the 
>>> following message:
>>>
>>> Program received signal EXC_BAD_ACCESS, Could not access memory.
>>> Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
>>> 0x0005389a in _d_newclass ()
>>>
>>> I also tried calling std.gc.setGCHandle(), but it also fails:
>>>
>>> Program received signal EXC_BAD_ACCESS, Could not access memory.
>>> Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
>>> 0x00053435 in std.gc.addRange(void*, void*) ()
>>>
>>> This may sound like a newbie question, but is there an easy way (or a 
>>> way at all) to develop a cross-platform application in D with shared 
>>> libraries?
>>>
>>> Dynamic linking is a very useful feature for software development. 
>>> Including this in the D specification (like threads) would be great.
>>>
>>> Hugues De Keyzer
>>>
>>> -- 
>>> The only constant in life is change
>>>    -- www.wikipedia.org
>>
>> GDC supports proper .so files on all platforms with .so files. Build 
>> the ..so files without a standard library (-no-stdlib or something 
>> like that) and then build the host exporting dynamic symbols (default 
>> on OS X, -rdynamic on everything else). DSSS, of course, does all of 
>> this for you.
>>
>> Windows DLL's will never be capable of full, proper support until 
>> phobos is in a .dll. Even then, if there was conflicting typeinfo, it 
>> could still fail. However, DDL ought to cover it there.
>>
>>  - Gregor Richards
> 
> Thank you for this quick reply. I tested on Mac OS X with -nostdlib and 
> had of course undefined references. After some more searching, I 
> succeeded in building a dynamic library with the following options:
> 
> gdc -dynamiclib -nostdlib -undefined dynamic_lookup -o mylib.so mylib.d
> 
> This seems to work. The GC works fine.
> 
> Another question: is it safe to directly load a mangled D symbol like this:
> 
> void* test = dlsym(handle, "_D8mymodule8MyModule4testFZv");
> 
> for static void MyModyle.test()?
> 
> It works, but will it work an all platforms?
> 

The mangling is the same on all platforms, so that should work, but is 
bound to break eventually. Better would be to make an extern (C) symbol 
in the loaded library.

  - Gregor Richards



More information about the Digitalmars-d mailing list