Can I use static constructors for registration?
Daniel Keep
daniel.keep.lists at gmail.com
Sun Mar 25 20:26:37 PDT 2007
Jason House Wrote:
> In C/C++, the linker will refuse to include a translation unit if
> there's no call from another translation unit that's already been
> accepted into the executable.
>
> Can the same thing occur in D? I know this is an obscure kind of
> problem. Here's some code that could be affected by this kind of issue.
>
> import myRegistrationMechanism.register
>
> static this(){
> register(functionNotUsedAnywhereElse);
> }
>
> void functionNotUsedAnywhereElse(inout int x){
> x += 17;
> }
If I remember correctly, OPTLINK (the linker DMD uses under Windows) does this, and gnu link has an option to do it.
Now, using the gnu link option (--gc-sections, I think) *does* cause problems. However, OPTLINK doesn't.
Also, with that example, all the functions *do* get called. Specifically, static this() gets called by the _moduleCtor function, which I believe is generated by the compiler at compile time.
So, long and short of it: yes. :)
-- Daniel
More information about the Digitalmars-d-learn
mailing list