D shared libraries

Sean Kelly sean at invisibleduck.org
Sat Apr 12 08:38:15 PDT 2008


Did you initialize the runtime within the shared library?  As for sharing
data between a D shared library and external code, if you want to pass
ownership of GCed data to the external code then the GC will have to
know either that it should just never collect the data or have a means
of scanning external thread stacks.  Tango opted for the second approach,
and you can register external threads via thread_attachThis() and
thread_detachThis() -- both are declared in the Thread module.

== Quote from BB (b.buderman at gmail.com)'s article
> Thanks very much, that worked.
> After being able to call into the .so, I see a segmentation fault at the
> end of main().  Through gdb I see it's crashing in _d_callfinalizer().
> Locally explicitly deleting the object allocated by the .so also results
> in a sigsegv.  However, calling into the .so to delete the object works
> fine.
> What is the reasoning for this - is it that the gc is statically linked
> into both the .so and the executable, so sharing a reference causes the
> local gc to try and clean it up?  If so, what are the guidelines/rules
> to use?
> Thanks again.
> Unknown W. Brackets wrote:
> > To compile a shared library, Linux using gdc:
> >
> > gdmd -op -oflibxyz.so.1.0.1 lib.d -fPIC -q,-rdynamic,-shared
> > gdmd -op main.d -fPIC -q,-rdynamic -L-ldl
> >
> > Compiling a shared library on Windows is a bit more complicated
> > (although possible with dmd and phobos as they are.) It involves DllMain
> > and def files...
> >
> > Please note, if you have selinux you may have have to run chcon.
> >
> > -[Unknown]
> >
> >
> > BB wrote:
> >> Tried this on D.gnu but didn't get an answer. Any feedback here?
> >> ------------------------------------------------------------------
> >>
> >>
> >> From what I read on newsgroups, this should be possible with gdc on
> >> linux? Goal is a plugin type solution. With the following code, I
> >> compile intf.d and lib.d together into a shared library, and intf.d
> >> and main.d into an executable. Compiles/links fine. When I run it, I
> >> get a message like:
> >>
> >> Null library handle: libxyz.so.1.0.1: undefined symbol: __data_start
> >>
> >> I compiled the .d files with -fPIC and the .so with:
> >> gcc -shared -Wl,-soname,libxyz.so.1 -fPIC.
> >>
> >> Is this definitely possible, and if so, what am I doing wrong? I tried
> >> gdc 0.24 and the latest off svn with the same results.
> >>
> >> TIA.
> >>
> >> -------------------------------------------------------------
> >>
> >> intf.d:
> >> =========
> >> module intf;
> >> public interface I { public int getId(); }
> >>
> >> lib.d:
> >> =========
> >> module lib;
> >> import intf;
> >> class A : I { public int getId() { return 42; } }
> >> extern (C) { I getObj() { return new A(); } }
> >>
> >> main.d:
> >> ==========
> >> ...
> >> void main(char[][] args)
> >> {
> >> void* hnd = dlopen(toStringz("libxyz.so.1.0.1"), RTLD_NOW);
> >> if (hnd == null) {
> >> printf("Null library handle: %s\n", dlerror());
> >> return 0;
> >> }
> >> ...
> >> }
> >>
> >>
> >>





More information about the Digitalmars-d mailing list