This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Global Constructors and Shared Libraries
> So collect should emit some "global initializer" into the library that
> __main can invoke, and therefore invoke each constructor in the library?
As I said: You should rather check whether you can have the system
linker collect things, e.g. by means of special sections.
> I don't see my version of collect doing this, so there must be some
> switches I'm missing in my build?
In write_c_file_stat, functions _GLOBAL__FI_<output file> are generated,
together with optional functions _GLOBAL__
> Also, what happens when one has a dependency chain such as:
>
> myprog --> sharedlib1 --> sharedlib2 --> libstdc++.sl --> libc.sl
>
> I assume that the initializer function must be uniquely named between
> the two libraries. What controls that? Does that also imply I can
> rebuild sharedlib2 without relinking myprog?
Again, it depends on your system. If you have initializer sections,
each library will properly initialize itself. On other systems,
linking myprog must determine that sharedlib1 needs sharedlib2
(e.g. using ldd), and put the sharedlib2 initializer into __main. On
yet other systems, the sharedlib1 initializer can contain a call to
the sharedlib2 initializer; the functions generated by collect2 are
protected against multiple invocation (I believe).
Regards,
Martin