[CMake] Combining two static libraries into one

Kyle Edwards kyle.edwards at kitware.com
Wed Jul 3 09:29:39 EDT 2019


On Wed, 2019-07-03 at 18:55 +0530, vinay kumar Kotegowder wrote:
>  I want to build lib2 static library with pre-built lib1 static
> library.
>  Does this work?
> 
>  add_library(lib1 STATIC lib1.c)
>  add_library(lib2 STATIC lib2.c)
>  target_link_libraries(lib2 INTERFACE lib1)

Yes, this will work.

>  Also, Kyle said linking against lib2 will link against both lib1 and
> lib2. Is there a static way of checking the relation between lib1 and
>  lib2 ? Separately and after using INTERFACE option with lib1 and
> lib2.

Asking "does lib1 link against lib2" has long been a requested feature
of CMake, but unfortunately, the existence of generator expressions
precludes this hypothetical feature. Because target_link_libraries()
supports generator expressions, which means the linking could change
based on the configuration (Debug, Release, etc.), we don't actually
know if lib1 links against lib2 until generate time, at which point
it's too late to do anything in configure time.

Kyle


More information about the CMake mailing list