[CMake] Adding external static library into a target static library

Pawel Veselov pawel.veselov at gmail.com
Thu Jun 15 22:19:44 EDT 2017


Hello.

I looked at this : https://cmake.org/Wiki/CMake/Tutorials/Object_Library,
and it suggests that I can use target_link_library() to combine static
libraries together. But I'm utterly failing at it.

Simple:
add_library(mylib STATIC ${lib_source})
target_link_libraries(mylib /path/to/libcool.a)

Doesn't work (libcool.a is not appended into mylib.a). If I want to link
against mylib (inside same cmake), then the libcool.a is added, however,
into the linkage of the executable that uses mylib.


I've tried making it more complicated by separating objects into an object
library, and then combining that object library and the external .a together
but it also doesn't do much:

add_library(mylib-obj OBJECT ${lib_source})
add_library(mylib STATIC $<TARGET_OBJECTS:mylib-obj> /path/to/libcool.a)

or

add_library(mylib-obj OBJECT ${lib_source})
add_library(mylib STATIC $<TARGET_OBJECTS:mylib-obj>)
target_link_libraries(mylib /path/to/libcool.a)

In all cases, cmake schedules ar on the object files, and ranlib on the
result.

Also, it seems it's not possible to create an empty library and add
other libraries as dependencies, which, in case of static libraries,
would make sense...

So, is is possible to, using cmake (and not do manual ar manipulations),
to create a static library containing objects from another static library,
external to the project?

Thank you!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170615/cd0a4b67/attachment.html>


More information about the CMake mailing list