Linux shared library loading/linking from C does not invoke (shared) static this

ichneumwn idonotenjoyemail at idonotenjoyemail.org
Tue Jan 12 09:31:08 UTC 2021


On Tuesday, 12 January 2021 at 08:19:45 UTC, ichneumwn wrote:
> Dear all,
>
> I was trying to invoke some D code from Python and ran into 
> issues which I eventually traced back to a simple example on 
> the D website itself :
>
> https://dlang.org/articles/dll-linux.html
>
> Particularly the section "Dynamically Loading a D DLL From a C 
> Program"
>
> In my case, and indeed already in 2013 
> (https://forum.dlang.org/post/yeqyqaaguhngczlnvqnq@forum.dlang.org), shared static this does not get invoked
>
> I normally use:
> LDC - the LLVM D compiler (1.21.0):
>   based on DMD v2.091.1 and LLVM 10.0.0   (under Linux)
>
> but I also tried
> DMD64 D Compiler v2.095.0
>
> Is the solution suggested in the linked post the "canonical 
> way"?
>
> Where could one file a suggestion for an update to the 
> documentation?
>
> Cheers!
>
> PS Enjoying my project in D especially how easy it is to get 
> the same code to run under windows too

Follow on to my own question: on Linux, with gcc, I have created 
the following file "starter.c" that I inject into my D shared 
library:

   int rt_init(void);
   int rt_term(void);

   // should really check for errors!
   static void __attribute__((constructor)) Dstarter(void) {
     rt_init();
   }

   static void __attribute__((destructor)) Dterminator(void) {
     rt_term();
   }

That seems to do the trick. Not sure how clean this is?





More information about the Digitalmars-d-learn mailing list