How to build a library directly from object files with CMake
-
17-04-2021 - |
Question
Given a set of object files, how would you build a static library with CMake ?
With autotools I would do
libXXX.a: $(OBJFILES)
$(AR) cru $@ $(OBJFILES)
$(RANLIB) $@
OBJFILES being a list of object files.
How to do that in CMake ?
EDIT: I can't recompile the original cxx files, I have to use the object files (*.o) to create the library.
Solution
Add a custom command, something like:
add_custom_command(OUTPUT libXXX.a COMMAND ${AR} cru ${OBJFILES} ).
If necessary you can use add_custom_target and add_dependencies to add your libXXX.a to a specific target, or perhaps to customize the dependencies.
OTHER TIPS
Use add_library
in CMakeLists.txt
:
add_library(XXX STATIC foo.c bar.cc baz.cxx)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow