[CMake] c++ and static initalizations

David Cole david.cole at kitware.com
Sat Jan 24 12:08:56 EST 2009


Alternatively, you could explicitly reference all the symbols of interest
from the main executable. That gives you just what you need, still allowing
the linker to strip out the unreferenced things you don't need.

Another benefit of this approach is that you'll end up with some code that
shows you the list of things you are actually pulling in to your executable.


HTH,
David


On Sat, Jan 24, 2009 at 12:01 PM, Timenkov Yuri <ytimenkov at gmail.com> wrote:

> This problem may occur with gcc because of static library linking policy.
> If you have a symbol in static library which is not referenced from main
> executable it will not be linked in. (For example, if you use static classes
> to register something to factories). To solve this issue, you should either
> link whole static lib (with --whole-archive linker option) or explicitly
> mark the symbol of interest as undefined (with --undefined option). I
> usually use first approach, because decorating C++ name is not trivial task.
>
>
> On Sat, Jan 24, 2009 at 6:00 PM, Aleix Pol <aleixpol at kde.org> wrote:
>
>> hi list,
>> I'm having a little problem involving c++ static initializations using
>> static libraries libraries. The problem is that if I link this code to the
>> executable as a static library the code is never run, but if it is a SHARED
>> library it is, but I need it to be static (if possible).
>>
>> The code looks like this:
>> struct AClass {
>>     static Object* registration() {...}
>>     static int Aregistered=something("A", AClass::registration);
>> };
>>
>> Any hint on what should I do so that it is called as i expect?
>>
>> Thanks,
>> aleix
>>
>> _______________________________________________
>> CMake mailing list
>> CMake at cmake.org
>> http://www.cmake.org/mailman/listinfo/cmake
>>
>
>
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090124/44d5c32e/attachment.htm>


More information about the CMake mailing list