3

I would like to merge several static lib files into one single lib using CMake and Visual Studio (lib.exe). I would like to pass a file list to the linker by setting

set_target_properties(biglib PROPERTIES STATIC_LIBRARY_FLAGS 
                             "/unknown/path/lib1.lib /unknown/path/lib2.lib")

However, the path is unknown at configure time. So I tried to use generator expressions:

set_target_properties(biglib PROPERTIES STATIC_LIBRARY_FLAGS 
                             "$<TARGET_FILE:lib1> $<TARGET_FILE:lib2>")

lib1 and lib2 are internal library targets defined somewhere else. The expression don't seem to be evaluated since the linker is searching for $<TARGET_FILE:lib1> which of course is not found.

I don't know if what I'm trying to do is going to work. Maybe someone can explain how to actually use generator expressions in such a case. Do I need to use add_custom_command somehow?

bender
  • 613
  • 1
  • 8
  • 23
  • 2
    According to documentation for [STATIC_LIBRARY_FLAGS](https://cmake.org/cmake/help/v3.0/prop_tgt/STATIC_LIBRARY_FLAGS.html), generator expressions are simply not allowed here (compare, e.g., with [COMPILE_DEFINITIONS](https://cmake.org/cmake/help/v3.0/prop_tgt/COMPILE_DEFINITIONS.html) property). It looks like `add_custom_command` would be more appropriate here: *merging* libraries is differs from *building* them. – Tsyvarev Jan 26 '16 at 09:24

2 Answers2

1

Merging static libraries is relatively complicated task and it is also platform and compiler dependent. For example, with Linux/gcc you need to extract all the objects from the libraries by ar and then combine them back to the merged library.

I'd recommend to use the macro MERGE_STATIC_LIBS from the libutils.cmake script, as mentioned here: https://stackoverflow.com/a/4736442/1274747

For Linux/Unix, you will need the merge_archives_unix.cmake.in as well.

Community
  • 1
  • 1
EmDroid
  • 5,918
  • 18
  • 18
0

You can define lib1 and lib2 targets as object libraries :

add_library(lib1 OBJECT ${lib1_src})
add_library(lib2 OBJECT ${lib2_src})
add_library(biglib STATIC $<TARGET_OBJECTS:lib1> $<TARGET_OBJECTS:lib2>) 
cromod
  • 1,721
  • 13
  • 26