www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can I use static constructors for registration?

reply Jason House <jason.james.house gmail.com> writes:
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;
}
Mar 25 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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
Mar 25 2007
parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Daniel Keep schrieb am 2007-03-26:
 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.

That isn't due to the linker used but how DMD generates the exception handling code: http://d.puremagic.com/issues/show_bug.cgi?id=879 Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFGB4jKLK5blCcjpWoRAoCDAJ0br2GiliMD3fbA+V5aG69SxAbf4wCglS6k 6eTv1E79Aj/E4N5VNyBasaI= =LR3b -----END PGP SIGNATURE-----
Mar 26 2007