[CMake] Newbie question: Static linking

Michael Jackson mike.jackson at bluequartz.net
Mon May 23 10:21:13 EDT 2011


On May 23, 2011, at 10:11 AM, Michael Wild wrote:

> On 05/23/2011 03:25 PM, Sanatan Rai wrote:
>> On 23 May 2011 13:38, Michael Wild <themiwi at gmail.com> wrote:
>>> On 05/23/2011 02:20 PM, Sanatan Rai wrote:
>>>> On 23 May 2011 12:54, Michael Jackson <mike.jackson at bluequartz.net> wrote:
>>>>> You might want to take a look at the Factory design pattern.
>>>>> 
>>>> 
>>>> That's exactly what I use...
>>>> 
>>>> --Sanatan
>>> 
>>> Yes, but you are registering the concrete factories implicitly instead
>>> of explicitly, which is causing you the trouble you experience.
>>> 
>>> Better have your user provide a function registering his/her classes
>>> explicitly.
>> 
>> I guess this is getting to be off topic, but indeed the
>> anonymous namespace trick is supposed to do exactly that.
>> 
>> I am not trying to be difficult here---just that it is not clear to me
>> that the solution to this problem is that straightforward.
>> 
>> When all the code files are linked in one monolithic bloc, everything
>> works correctly. It is when one starts dividing them into individual
>> libraries that this problem occurs. I haven't seen a solution to this
>> problem either in books or via google.
>> 
>> --Sanatan
> 
> The problem is, that when you link a static library to another binary
> (be it shared library or executable) only the *required* symbols are
> used, all others get discarded. Since nothing in your code actually
> references those global instances in the anonymous namespace (the linker
> doesn't care about that, BTW), they are ignored.
> 
> Four solutions:
> 
> 1. Only do monolithic builds.
> 2. Use shared libraries/DLLs
> 3. Use --whole-archive or similar and hack your way through MSVC (I did
> it once. It was ugly. Very ugly. See
> https://github.com/themiwi/cppcheck/tree/227378f763d50b005b7dd2167e2cef791054a30c.
> Especially lib/CMakeLists.txt and lib/generateStaticLinkFlags.cmake. I
> replaced it with an explicit registration scheme now...)
> 4. Use an explicit registration scheme.
> 
> For sanity's sake, go with 4.
> 
> Michael


I use "4" in my own code and everything "just works" and I have the same type of setup as the original poster. I have a few of my own concrete classes and the user can create new ones. They just have to "register" their own new classes in addition to calling the "RegisterKnowFactories()" method first. This ensures everything links correctly and is not that much to ask your programmers to do. I think VTK/ITK/ParaView may also use these types of design patterns.
___________________________________________________________
Mike Jackson                      www.bluequartz.net
Principal Software Engineer       mike.jackson at bluequartz.net 
BlueQuartz Software               Dayton, Ohio




More information about the CMake mailing list