thestar
thestar

Reputation: 5239

Copying executable and DLLs to User specified location using CMake in windows

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?

Upvotes: 1

Views: 1024

Answers (2)

Antwane
Antwane

Reputation: 22598

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.

Upvotes: 4

thestar
thestar

Reputation: 5239

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

Upvotes: 1

Related Questions