Thread overview
equivalent of __attribute__((constructor))
May 23, 2013
Ellery Newcomer
May 23, 2013
Jacob Carlborg
May 24, 2013
Ellery Newcomer
May 24, 2013
Jacob Carlborg
May 24, 2013
Ellery Newcomer
May 24, 2013
Johannes Pfau
May 23, 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.
May 23, 2013
On 2013-05-23 06:27, Ellery Newcomer wrote:
> 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.

I don't know if it's automatically linked but here you go:

https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dylib_fixes.c

-- 
/Jacob Carlborg
May 24, 2013
On 05/22/2013 11:18 PM, Jacob Carlborg wrote:
> On 2013-05-23 06:27, Ellery Newcomer wrote:
>
> I don't know if it's automatically linked but here you go:
>
> https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dylib_fixes.c
>
>

posix.mak makes no reference to it
May 24, 2013
On 2013-05-24 02:02, Ellery Newcomer wrote:

> posix.mak makes no reference to it

Then I guess it's not used. Just compile it manually and link with it. I don't think that D has anything corresponding to __attribute__((constructor)). I also see a problem with adding such feature. It will be run before the runtime is initialized (that's how it works now). Then people will start using it and complain about there code failing in mysterious ways because the runtime isn't initialized.

-- 
/Jacob Carlborg
May 24, 2013
Am Wed, 22 May 2013 21:27:00 -0700
schrieb Ellery Newcomer <ellery-newcomer@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.
May 24, 2013
On 05/23/2013 11:39 PM, Jacob Carlborg wrote:
> On 2013-05-24 02:02, Ellery Newcomer wrote:
>
>> posix.mak makes no reference to it
>
> Then I guess it's not used. Just compile it manually and link with it. I
> don't think that D has anything corresponding to
> __attribute__((constructor)). I also see a problem with adding such
> feature. It will be run before the runtime is initialized (that's how it
> works now). Then people will start using it and complain about there
> code failing in mysterious ways because the runtime isn't initialized.
>

so don't document it :)