equivalent of __attribute__((constructor))

Ellery Newcomer ellery-newcomer at utulsa.edu
Wed May 22 21:27:00 PDT 2013


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.


More information about the Digitalmars-d-learn mailing list