[BUG] ld behavior varies for C++ static initializer depending on .a or .o input

Hal Black hablack@vt.edu
Sat Apr 12 04:01:00 GMT 2003


Alexandre Oliva wrote:
> On Apr 11, 2003, Hal Black <hablack@vt.edu> wrote:
> 
> 
>>But when linking .a files composed
>>of those .o files, static initializers are not called.
> 
> 
> And, if you look closer, you'll notice the object files that contain
> the initializers aren't linked in at all.  Which is perfectly fine,
> given that no symbols from it are referenced.  Maybe you want
> --whole-archive?
> 

Thanks Alexandre, for replying and the --whole-archive tip - it works 
great (has to be -Wl,-whole-archive when passed to g++, though, as noted 
on the man page).

I still believe this is a bug, however.  Even though there aren't any 
references to static intializers, there is an implicit reference to 
them.  When using .o files, there are no references either, but the 
static intializer is included in that case.  Why should it be different 
for a .a file with the same .o files in it?  Should .a files not be used 
for C++?

Perhaps the solution is to pass --whole-archive by default to ld when 
linking with g++.

But, at least there is a workaround.

Thanks also to Alan for the reference, I don't know how I missed that 
thread.



More information about the Binutils mailing list