Linkage order in Linux

Nick Clifton nickc@redhat.com
Thu Feb 11 12:18:00 GMT 2010


Hi Michael,

>> I have seen similar behavior with dueling __attribute__((constructor))
>> functions, in which case the simple fix (besides massaging the link
>> order) was to give the constructors an appropriate priority.

> The only __attribute__ I found was:
> define DLLEXP extern "C" __attribute__ ((visibility("default")))
> I replaced it with:
> define DLLEXP extern "C"
> but that didn't help.

Ah, but if we are talking C++ libraries then it could still be a 
constructor priority issue.  Matt was referring to this G++ extension to 
the C++ language:  [Text taken from the GCC documentation.]

   In Standard C++, objects defined at namespace scope are
   guaranteed to be initialized in an order in strict accordance
   with that of their definitions *in a given translation unit*.
   No guarantee is made for initializations across translation
   units.  However, GNU C++ allows users to control the order of
   initialization of objects defined at namespace scope with the
   "init_priority" attribute by specifying a relative *priority*,
   a constant integral expression currently bounded between
   101 and 65535 inclusive.  Lower numbers indicate a higher
   priority.

   In the following example, A would normally be created before
   B, but the "init_priority" attribute has reversed that order:

     Some_Class  A  __attribute__ ((init_priority (2000)));
     Some_Class  B  __attribute__ ((init_priority (543)));

   Note that the particular values of "priority" do not matter;
   only their relative ordering.

So what you need to do is to identify the constructors in the Horde3D 
library that need to be run before constructors in the GL library and 
use this attribute to give them a priority.  (I believe that the default 
priority for unattributed constructors is 65535).

Cheers
   Nick



More information about the Binutils mailing list