Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 10, 2013 Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
What's the current status of dynamic loading (and/or linking) for D libraries? I've been playing with this a bit in hopes of calling a D shared library from R through the C ABI, and noticed that on Windows it seems fine, but on Linux dmd won't build my library unless it includes main(), and it segfaults if I call writeln(). I've seen this thread: http://forum.dlang.org/thread/hmhaldyfziejrplgzazt@forum.dlang.org And also this one: http://forum.dlang.org/thread/k3vfm9$1tq$1@digitalmars.com What I gather is that right now, DLLs work on Windows, but on *nix, shared libraries cannot yet use anything from Phobos. Am I correct that it's safe to dynamically load a D library on Windows? How out-of-date is the information in the tutorial for making DLLs on dlang? Is supporting dynamically loadable libraries a priority for the developers, or something that might not happen for a while? |
February 10, 2013 Re: Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Ulle | > Is supporting dynamically loadable libraries a priority for the developers, or something that might not happen for a while? Judging by this thread: http://forum.dlang.org/thread/kf1223$oo8$1@digitalmars.com It has just become a priority. |
February 10, 2013 Re: Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Ulle | On Sunday, 10 February 2013 at 22:11:52 UTC, Nick Ulle wrote:
> What's the current status of dynamic loading (and/or linking) for D libraries?
>
> I've been playing with this a bit in hopes of calling a D shared library from R through the C ABI, and noticed that on Windows it seems fine, but on Linux dmd won't build my library unless it includes main(), and it segfaults if I call writeln().
>
> I've seen this thread:
> http://forum.dlang.org/thread/hmhaldyfziejrplgzazt@forum.dlang.org
>
> And also this one:
> http://forum.dlang.org/thread/k3vfm9$1tq$1@digitalmars.com
>
> What I gather is that right now, DLLs work on Windows, but on *nix, shared libraries cannot yet use anything from Phobos.
>
> Am I correct that it's safe to dynamically load a D library on Windows? How out-of-date is the information in the tutorial for making DLLs on dlang?
>
> Is supporting dynamically loadable libraries a priority for the developers, or something that might not happen for a while
For shared libraries (linux) I'd use ldc . It's just works.
|
February 10, 2013 Re: Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Sunday, 10 February 2013 at 23:41:54 UTC, John Colvin wrote:
> For shared libraries (linux) I'd use ldc . It's just works.
Not quite. Using
ldc2 -shared -relocation-model=pic mylib.d
Compiles and links, but on loading there are undefined symbols (_Dmain). Including a main() function gets me a loadable library, but much like the case with dmd, it segfaults if I call writeln.
|
February 10, 2013 Re: Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
Posted in reply to jerro | On Sunday, 10 February 2013 at 22:46:29 UTC, jerro wrote:
>
> Judging by this thread:
>
> http://forum.dlang.org/thread/kf1223$oo8$1@digitalmars.com
>
> It has just become a priority.
Thanks! This is great to hear. I didn't come across that thread while searching the forums before.
|
February 10, 2013 Re: Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Sunday, 10 February 2013 at 23:41:54 UTC, John Colvin wrote:
> For shared libraries (linux) I'd use ldc . It's just works.
You are living a dangerous life. ;)
It appears to work at first, but the test suite is known to segfault in random places with druntime/Phobos being built as a shared library, most probably because the GC/threading code doesn't really handle dynamic libraries at all.
David
|
February 11, 2013 Re: Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Ulle | You can arrange to have d's runtime initialized when dlopen loads the shared lib by adding a small shim that uses gcc attributes, as follows: // shim.c __attribute__((constructor)) static void dinit() { rt_init(); } __attribute__((destructor)) static void dfini() { rt_term(); } // end of shim.c Compile this to a .o with gcc, then include the .o in the build. Note that I've only tried this trick with a simple "hello world" shared lib, so I apologize in advance if it doesn't work for real-world uses. |
February 11, 2013 Re: Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Ulle | On Sunday, 10 February 2013 at 22:11:52 UTC, Nick Ulle wrote: > What's the current status of dynamic loading (and/or linking) for D libraries? > > I've been playing with this a bit in hopes of calling a D shared library from R through the C ABI, and noticed that on Windows it seems fine, but on Linux dmd won't build my library unless it includes main(), and it segfaults if I call writeln(). Dynamic linking works in linux (at least observed features work, see example http://forum.dlang.org/thread/k3vfm9$1tq$1@digitalmars.com?page=2). Dynamic loading does not work. > Is supporting dynamically loadable libraries a priority for the developers, or something that might not happen for a while? Yes, couple of days ago Walter announced that shared libraries is priority and he will get into this after releasing new dmd version. |
February 13, 2013 Re: Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Ulle | On 02/10/2013 11:11 PM, Nick Ulle wrote: > What's the current status of dynamic loading (and/or linking) for D > libraries? > No dynamic loading in sight yet. We need to extend the GC, find semantics/techniques for TLS initialization, provide tools to load symbols ... What is on the way but will take some more weeks is link-time support for shared libraries. https://github.com/D-Programming-Language/druntime/pull/395 > I've been playing with this a bit in hopes of calling a D shared library > from R through the C ABI, and noticed that on Windows it seems fine, but > on Linux dmd won't build my library unless it includes main(), and it > segfaults if I call writeln(). > The reasons for your crashes are missing initialization. You could experiment with calling rt_init/rt_term which are exported from your shared library, but only if you're not threaded. > What I gather is that right now, DLLs work on Windows, but on *nix, > shared libraries cannot yet use anything from Phobos. > Yes, there is rudimentary support on Windows, but you'll inevitably run into ODR issues. http://d.puremagic.com/issues/show_bug.cgi?id=7020#c4 > Am I correct that it's safe to dynamically load a D library on Windows? > How out-of-date is the information in the tutorial for making DLLs on > dlang? > AFAIK it should work. > Is supporting dynamically loadable libraries a priority for the > developers, or something that might not happen for a while? It's a huge priority but also a huge undertaking. |
February 13, 2013 Re: Status of Dynamically Loadable D Libraries | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | On Sunday, 10 February 2013 at 23:52:30 UTC, David Nadlinger wrote: > On Sunday, 10 February 2013 at 23:41:54 UTC, John Colvin wrote: >> For shared libraries (linux) I'd use ldc . It's just works. > > You are living a dangerous life. ;) > > It appears to work at first, but the test suite is known to segfault in random places with druntime/Phobos being built as a shared library, most probably because the GC/threading code doesn't really handle dynamic libraries at all. > > David Does this pretty much completely invalidate projects like this: https://bitbucket.org/ariovistus/pyd ? I've had success using it, as well as loading d shared libraries from C/IDL, with the appropriate rt_init calls etc. |
Copyright © 1999-2021 by the D Language Foundation