equivalent of __attribute__((constructor))

Johannes Pfau nospam at example.com
Fri May 24 11:13:20 PDT 2013


Am Wed, 22 May 2013 21:27:00 -0700
schrieb Ellery Newcomer <ellery-newcomer at utulsa.edu>:

> In the context of shared libraries, with gcc
> 
> __attribute__((constructor))
> void myfunc() { .. }
> 
> is used to make myfunc be called upon loading of the shared library
> (you can tell I know what I am talking about here) via some field in
> the ELF headers, apparently. Is there any way to get our trusty d
> compilers to do the equivalent?
> 
> Sure, Ellery, we have this awesome feature called module
> constructors. Check em out.
> 
> Ehh, I would be using this to initialize druntime...
> 
> You could just define _init, couldn't you?
> 
> Yes, but there is only one _init, while the above can be used with 
> multiple functions and thus wouldn't inadvertently cause important
> code to not run. If I don't have to, I'd rather not.
> 
> Wait, why are you initializing druntime?
> 
> Because druntime isn't set up to do it yet for c main calling d
> shared lib. You'd think it would need the same sort of functionality
> when it does implement it, though.

LDC has got #pragma(LDC_global_crt_[c/d]tor) for this, see
http://wiki.dlang.org/LDC-specific_language_changes#Pragmas

We will at some point implement something similar in gdc. I don't know
about dmd though.


More information about the Digitalmars-d-learn mailing list