[CMake] shared and static library

Darko Miletic darko at uvcms.com
Thu Feb 2 09:31:42 EST 2006


Xavier Delannoy wrote:
> hi all, 
> 
> Is there any proper way to build a shared AND static lib with cmake ?

Not directly from the same script. You will have to write two separate
scripts for that. One that will produce static lib and another that will
produce shared lib. It can be represented like this:

<root_source_dir>
|
|--<srcdir>
|
|--<static_lib_dir> (script goes here)
|
|--<shared_lib_dir> (script goes here)

So in static_lib_dir CMakeLists.txt will be like this:

SET (SRCS a.cpp b.cpp ... x.cpp)

ADD_LIBRARY(<libname> STATIC ${SRCS})

And in shared_lib_dir CMakeLists.txt will be like this:

SET (SRCS a.cpp b.cpp ... x.cpp)

ADD_LIBRARY(<libname> SHARED ${SRCS})

Or even better you can have one file that lists source files in src
folder and include that one in both scripts like this:

sources.txt:

SET(SRC a.cpp b.cpp ... x.cpp)


So in static_lib_dir CMakeLists.txt will be like this:

INCLUDE(${CGILib_SOURCE_DIR}/src/sources.txt)

ADD_LIBRARY(<libname> STATIC ${SRCS})

And in shared_lib_dir CMakeLists.txt will be like this:

INCLUDE(${CGILib_SOURCE_DIR}/src/sources.txt)

ADD_LIBRARY(<libname> SHARED ${SRCS})

But you get the idea.




More information about the CMake mailing list