I am new to CMake. I have got my project compiling. I have a structure like this
PROJECT
-
SRC
- test
.cpp (all source files) and CMakeLists.txt for this folder (creating a static library) - example
.cpp (all source files) and CMakeLists.txt for this folder (creating a static library)
- test
-
Include
- test
.h (all the header files) - example
.h (all the header files)
- test
-
build
- CMakeLists.txt (Main CMakelist file)
-
lib
- test (contains the generated files)
- example (contains the generated files)
The question is how do I copy all static files ie. from test and example folder and place them in a different folder outside the binary structure recursively?
My main CMakeLists.txt file:
PROJECT(copythefiles)
SET(CMAKE_CURRENT_BINARY_DIR ".")
add_subdirectory(/src/test /lib/test) # I am specifying the location where the files are to be generated
add_subdirectory(/src/example /lib/example)
ADD_CUSTOM_TARGET( a ALL COMMAND ${CMAKE_COMMAND} -E echo "\{X}" > ${CMAKE_CURRENT_BINARY_DIR}/lib/test/libtest.a )
ADD_CUSTOM_COMMAND(TARGET a POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_BINARY_DIR}/lib/test/libtest.a
${CMAKE_CURRENT_BINARY_DIR}/lib/libtest.a )
This copies the files. But I have around 20 projects and I would like to do it recursively.
Thanks 🙂
It’s normal to have your top-level CMakeLists.txt file in the root of your project. If you move your CMakeLists.txt out of “/build” to the root, then you should be able to call
add_subdirectorywithout having to specify the binary path for each case.Assuming you move the CMakeLists file, then you can insert this before the
add_subdirectorycalls:This then allows you to add e.g.
MoveLib(my_test)inside the libraries’ CMakeLists.txt files, wheremy_testis the name of the library concerned.A copy of all libraries will then end up in “/lib”. If you’re not really wanting copies, then you should have a look at the
ARCHIVE_OUTPUT_DIRECTORYproperty. If you simply addthen all your static libraries will end up in “/lib”. There are a couple of things to watch for here though.
Shared libraries aren’t covered by
ARCHIVE_OUTPUT_DIRECTORY. The details for shared libs are in the docs forARCHIVE_OUTPUT_DIRECTORYthough.Also, some generators (e.g. MSVC) append a per-configuration subdirectory to the specified directory, so you’d end up with “/lib/Debug”, “/lib/Release”, etc. You can circumvent this by setting the configuration-specific versions of
CMAKE_ARCHIVE_OUTPUT_DIRECTORYto all point to${CMAKE_SOURCE_DIR}/lib: