In CMake, I would like to run post build command which copies executable and required dll to User specified location automatically. Is it doable using CMake?
-
1Do you mean [install](http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:install) ? (: – Dec 23 '13 at 23:10
-
yes, but do you have good example that I can use? – thestar Dec 23 '13 at 23:11
2 Answers
It depends what do you want to do. Here is 4 different solutions. There is probably others to add to this list...
install() command
If you want to copy executable and dll you just built you can use the install()
command but it will work only when the user run make install
.
Setting variables
If you want to do it directly at build time, you can use CMake variables to configure your build. These variables are described at http://www.cmake.org/Wiki/CMake_Useful_Variables
EXECUTABLE_OUTPUT_PATH
set this variable to specify a common place where CMake should put all executable files (instead of CMAKE_CURRENT_BINARY_DIR)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
LIBRARY_OUTPUT_PATH
set this variable to specify a common place where CMake should put all libraries (instead of CMAKE_CURRENT_BINARY_DIR)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
A custom command
If you want to copy other executable or dll you did not build yourself (binary libs, etc.), a good solution is to use a custom command. In that case it could be very tricky to have a portable solution working on all os to copy files. That's why CMake provide this feature (with others) directly from its cmake
executable:
In command line you could use that:
cmake -E copy_if_different <SOURCE> <DESTINATION>
Don't forget you can call cmake executable from a CMakeLists file using ${CMAKE_COMMAND}
variable ;)
configure_file() command
And to finish, the configure_file()
command allows you to produce a file from another by replacing variables by their value in the target file. If the source file does not contains variables, the target file is only a copy of the source. But I don't know if it works perfectly with binary file. You should carefully test that by yourself.

- 20,760
- 7
- 51
- 84
-
Thanks buddy. can you help me in this question. http://stackoverflow.com/questions/20767436/create-a-directory-based-on-release-debug-build-type-in-cmake – thestar Dec 24 '13 at 22:38
I added the custom command.
add_custom_command( TARGET FOLDER POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different SOURCE DESTINATION")
and it copies DLLs.
To copy executable, I used simple INSTALL command.
http://www.cmake.org/Wiki/CMake:Install_Commands#New_INSTALL_Command

- 4,959
- 2
- 28
- 22