[CMake] c++ and static initalizations

Timenkov Yuri ytimenkov at gmail.com
Sat Jan 24 12:01:27 EST 2009


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090124/0076e6b7/attachment.htm>


More information about the CMake mailing list