[CMake] Adding -fPIC to static libraries

Brad King brad.king at kitware.com
Wed Mar 1 15:06:23 EST 2006


Prakash Punnoor wrote:
> Actually I don't know. :-) Or perhaps I confused its behaviour while building 
> a project as such. Here at least it is no problem if you have some libtool 
> libs declared within the porject and link them either static or shared with 
> executables or libs within the same project. I have to test whether it also 
> works once you have installed the libs.
> 
> Would this work with cmake? Don't I have to work-around this by compiling 
> seperate static / shared libs and explicitly state which of the ones I want 
> to link in?

Supporting this in CMake is tricky.  The problem we face that libtool 
does not is supporting Windows compilers (correct me if I'm wrong I have 
not checked this).  On Windows a shared library "foo" consists of the 
runtime library "foo.dll" and the import library "foo.lib", and a static 
library "foo" consists of the file "foo.lib".  Therefore the static and 
shared versions of the library cannot have the same name.  Cygwin and 
MinGW deal with this by naming the static library "libfoo.a" and the 
import library "libfoo.dll.a".  I'm not sure this is an option for 
Windows compilers that expect "foo.lib" in both cases.

The current CMake way to support static and shared libraries is to use 
two ADD_LIBRARY commands with different target names:

ADD_LIBRARY(foo-shared SHARED ${FOO_SOURCES})
ADD_LIBRARY(foo-static STATIC ${FOO_SOURCES})

The shared one will build the objects with -fPIC and the static one will 
not.  In the future I hope to add a better solution.

-Brad


More information about the CMake mailing list