CMake post-build-event: copy compiled libraries

Currently, the binary directory structure of my project (Windows):

bin/mainProject/{Debug,Release} bin/library1/{Debug,Release} bin/library2/{Debug,Release} ... bin/libraryN/{Debug,Release} 

I would like to copy the libraries library1lib.dll , ... libraryNlib.dll to the bin/mainProject/{Debug,Release} directory after building them.

For CMake, I think this is doable using the post-build event, so I tried to add this to each of the CMakeLists.txt libraries:

 add_custom_command(TARGET library1 POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/library1lib.dll ${CMAKE_BINARY_DIR}/mainProject/${CMAKE_BUILD_TYPE}/ ) 

There are currently two problems:

  • ${CMAKE_BUILD_TYPE} does not seem to be defined, at least I get an empty string for this variable in the output window.
  • Is it possible to make this event after assembly more general? How to replace the actual dll name with some variable?
0
source share
1 answer

You can make this more general with generator expressions :

 add_custom_command( TARGET library1 POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:library1> $<TARGET_FILE_DIR:mainProject>/$<TARGET_FILE_NAME:library1> ) 

Alternative

You can - if every dependency is built in your CMake project - also just specify a common output path for all executable files and DLLs with something like:

 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Out") 

Note. An absolute path is required here because otherwise it would be relative to the default output path of each target. Note that the configuration subdirectory is automatically added by CMake.

References

0
source

Source: https://habr.com/ru/post/1271775/


All Articles