[CMake] Newbie question: Static linking

Michael Hertling mhertling at online.de
Mon May 23 22:08:49 EDT 2011


On 05/23/2011 08:42 PM, Sanatan Rai wrote:
> On 23 May 2011 17:46, Michael Hertling <mhertling at online.de> wrote:

>> In summary, this whole issue is not related to C++ or even to CMake,
>> but to the manner static libraries are handled: The linker - at least
>> the GNU one - picks out entire object files, or drops them if they are
>> not referred to. This is something one must keep in mind, particularly
>> when dealing with global objects for initialisation purposes. BTW, also
>> keep in mind that the order of initialisations, i.e. constructor calls,
>> among global objects is unspecified; this might become important when
>> such global objects refer to each other.
> 
> I do realise that it was off-topic, but people very kindly offered suggestions!
> So one had to go on!
> 
> Many thanks to all for a careful and useful discussion.

Please don't get me wrong on this point: Far be it from me to criticise
your concern as off-topic; on a build system's mailing list, it rather
isn't, IMO. Instead, I just wanted to point out that this issue is not
immediately related to CMake or C++ but to the implications of static
libraries. Of course, we can frankly discuss it here, especially how
to address it with the means of CMake.

> Unfortunately, I am stuck with the paradigm of having to kludge loading an
> entire library. For various reasons the one may not have a reference to
> the hosted object in main, which must remain agnostic. Indeed, in my particular
> line of business (finance), this happens to be a `standard' pattern.
> Whether good or bad is a discussion that'd be too off topic...(though am happy
> to continue the discussion off/on list if people are so inclined!).

In this thread, you have spoken about "loading a library", "library
loaded when needed" and "library loaded in memory", e.g., but static
libraries aren't loaded in this sense; they're examined at link time,
and that's it. May it be possible that shared libraries are what you
actually want, i.e. is there any reason why the libraries in question
must be static? Note that shared libraries don't have the limitations
of static ones; say, they are not "cherry-picked" w.r.t. object files.

If you really want to have actions performed at the program's startup
without any explicit ado, you might take a look at GCC's constructor/
destructor attributes, but be aware that this is highly dependent on
the underlying binary format and the corresponding development tools.

Regards,

Michael


More information about the CMake mailing list