[Cmake] How do I link a static library into a library

Brad King brad.king at kitware.com
Wed, 21 Apr 2004 14:29:14 -0400


Stuart MacKay wrote:
> I have a project that generates a C++ library which I am migrating to 
> CMake from the GNU autotools. The library uses functions from an 
> external C library (zlib) which are only used internally. I have been 
> trying to get cmake to link in the external library code so I do not 
> have to distribute it with the project library.
> 
> The project is structured as follows:
> 
> project-root
> project-root/library-source
> project-root/lib/libz.a
> 
> The project-root contains a CMakeLists.txt with:
> 
> PROJECT(MyProject)
> LINK_LIBRARIES(lib/libz.a)
> SUBDIRS(library-source)
> 
> then the library-source directory contains CMakeLists.txt which contains 
> the lines:
> 
> SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__cplusplus")
> ...
> ADD_LIBRARY(mylib STATIC ${SRCS})
> 
> I have tried various combinations of specifying the external library 
> with either LINK_LIBRARIES command and TARGET_LINK_LIBRARIES. In all 
> cases the zlib functions are not linked in with the library.
> 
> Do I need to consider the project structure when choosing which 
> CMakeLists.txt file to put commands in ? Or am I missing something 
> fundamental when using cmake ?
> 
> A canonical example would be greatly appreciated.

As far as I know, there is currently no way to get CMake to tell the 
linker to copy an entire static library into another.  I do not know 
whether this is even possible on all platforms (like windows).

For static libraries, all linking does is to add a dependency in the 
project so that whenever something links to mylib it automatically links 
to zlib also.

-Brad