[CMake] portable way for specifying whether everything or selective libs should be linked statically or as shared objects?

Bill Hoffman bill.hoffman at kitware.com
Mon Apr 2 21:57:26 EDT 2012


On 4/2/2012 9:18 PM, Christoph Anton Mitterer wrote:
> Hi.
>
> I started to switch over all my build systems to CMake recently and am
> stuck with the following.
>
> I thought of the "C" in CMake ("cross-platform") that it means to that
> most basic tasks are handled for most platforms/compilers in a
> transparent way, but this seems all but be the case unfortunately.
Nothing is perfect.  :)
>
> This starts with commands like add_definitions where one have to use
> "-D" as parameter suffix (well eventually there could be compilers which
> don't use -D as option for this)... and ends with the following what I
> though would really be one of the basics in business.
I was concern about this for a while, however, we have never seen a 
compiler that did not take -D.  :)   Actually, I ported to a VMS 
compiler once and it used a different flag, but I was able to fix it 
from the CMake source level, translating the -D to the correct flag for 
that compiler.  So, -D is not really an issue, as CMake can correct for 
it if need be.   The CMake api for definitions is -D.
>
> I'd like to allow users of my build system to specify whether
> - everything shall be linked statically (i.e. what gcc's --static does)
> - selectively select libraries (for which I allow it) to be linked
> statically as shared objects or to be dynamically loaded[0].
>
> Isn't this possible? If it is how?
>
> I mean I've googled obviously and found ugly looking hacks where you set
> some magic variables to --static (e.g.)... but this is then absolutely
> importable.
>
> Rather I'd have expected some clean interface e.g.
> target_link_libraries(foo STATIC lib1 lib2 SHARED lib3 lib4)
> where obviously lib1/2 are linked statically and 3/4 as shared objects.

That would be nice, and has been on the todo list for a while.  However, 
it has never been important enough for someone to actually implement.
>
>
> Any ideas?! :-)
Well, you could implement it... :)

This is an interesting interface:
target_link_libraries(foo STATIC lib1 lib2 SHARED lib3 lib4)

I would suggest that you move to the discussion to the cmake-developers 
list.

-Bill



More information about the CMake mailing list