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?

1
  • 2
    According to documentation for STATIC_LIBRARY_FLAGS, generator expressions are simply not allowed here (compare, e.g., with COMPILE_DEFINITIONS property). It looks like add_custom_command would be more appropriate here: merging libraries is differs from building them. Commented Jan 26, 2016 at 9:24

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The OP clearly states that cross-platform compatibility is not necessary ("using CMake and Visual Studio").
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>) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.