[CMake] linked to static instead of shared library

Brad King brad.king at kitware.com
Sat Mar 5 09:33:21 EST 2005


David Somers wrote:
> At the moment I'm using FIND_LIBRARY to pick up the location of a shared 
> library. Is there a way to get it to pick up the static library instead? 
> (Yes, both the static and shared libraries coexist in the same directory, and 
> I can't really change that becasue its a third-party library and I don't want 
> to muck around with its build system).
> 
> For static libraries, is Link Library still the way to specify it?

There are several issues here.  First, there is currently no way to get 
FIND_LIBRARY to pick up only a shared or only a static library, though 
this wouldn't be a bad feature request.  Second, no matter whether the 
library found is shared or static, the link line that is generated will 
be split into -L and -l options.  For example:

TARGET_LINK_LIBRARIES(myexe /home/me/lib/libfoo.a)

will result in a link line for myexe containing

  -L/home/me/lib -lfoo

This split is necessary to prevent the linker from copying the entire 
static library into the executable instead of just picking out the 
object files needed.  Unfortunately it also lets the linker choose 
between the static and the shared library.  By default most linkers will 
choose the shared version.

You have to specifically add flags to tell it to build a static 
executable.  You can either use the variable CMAKE_EXE_LINKER_FLAGS to 
set the flags for all executables, or set the LINK_FLAGS target property 
on a specific executable.  Unfortunately right now there is no variable 
that is automatically set with the appropriate flags for the platform, 
so you need a bunch of IF conditions (and maybe your own try-compiles) 
to set it.  We should probably add a target property for executables to 
specify whether it is linked statically or shared.

Please add separate feature requests for the find and link stages here:

http://www.cmake.org/Bug

Thanks,
-Brad


More information about the CMake mailing list