[CMake] static & shared library

Brad King brad.king at kitware.com
Wed Aug 3 13:51:38 EDT 2005


William A. Hoffman wrote:
> Yes, that is correct, although many autotools projects I have seen work the way cmake does.
> I guess you could do something like this:
> 
> 
> SET(FOO_SRCS ...)
> ADD_LIBRARY(fooStatic STATIC ${FOO_SRCS})
> ADD_LIBRARY(fooShared SHARED ${FOO_SRCS})
> SET_TARGET_PROPERTIES(fooStatic fooShared PROPERTIES OUTPUT_NAME foo)
> 
> I am not sure TARGET_LINK_LIBRARIES would work correctly, but it would
> build a libfoo.so and a libfoo.a in the same build.

In CMake 2.2 this will probably not work.  The link line generated by 
the makefile generator now removes all possible names for a library 
(shared, static, versioned shared, etc.) before linking the final 
library.  The reason is that if someone using BUILD_SHARED_LIBS switches 
from shared to static then the static library will get built but unless 
the shared is removed then linking to the library will choose the 
out-of-date shared one.

For the same reason it is tricky to have a shared and static library 
with the same name in the same directory in one's build tree.  I 
personally think it is generally a bad idea to have a shared and static 
library with the same name because one then needs complicated linker 
flags to choose the proper library:

cc -o myexe myexe.o -lmylib_static

versus

cc -o myexe myexe.o -Wl,-Bstatic -lmylib -Wl,-Bshared

and of course those -Wl,-B options are platform-specific.

-Brad


More information about the CMake mailing list