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

Mike Parker aldacron at gmail.com
Tue Jan 12 09:49:46 UTC 2021


On Tuesday, 12 January 2021 at 09:31:08 UTC, ichneumwn wrote:

>
> 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?

You should be able to do the same in D with 
`pragma(crt_constructor)` and `pragma(crt_destructor)`:

https://dlang.org/spec/pragma.html#crtctor
https://dlang.org/spec/pragma.html#crtdtor
https://dlang.org/changelog/2.078.0.html#crt-constructor




More information about the Digitalmars-d-learn mailing list