5

How can I make CLion automatically copy my compiled executable to a specified directory after each build?

Since CLion uses CMake, I guess this should be possible with some CMake command in the CMakeLists.txt file. But I don't know how to do it.

Leos313
  • 5,152
  • 6
  • 40
  • 69
a06e
  • 18,594
  • 33
  • 93
  • 169

3 Answers3

20

I don't know about CLion but generally you can add this as a post-build step to an executable target in CMake with:

add_executable(MyExe ...)
add_custom_command(TARGET MyExe 
                   POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:MyExe> SomeOtherDir)

See e.g. Copy target file to another location in a post build step in CMake

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • Can you explain what I am supposed to fill in here? – a06e Jul 07 '15 at 19:15
  • 1
    MyExe is your executable's target name, in my example from `add_executable(MyExe ...)`. You just add the above custom command after the `add_executable()` line to your `CMakeLists.txt`. It will take the full path of your executable (see [generator expressions](http://www.cmake.org/cmake/help/v3.2/manual/cmake-generator-expressions.7.html#informational-expressions)) and will copy it to `SomeOtherDir`. If you don't give a full path as `SomeOtherDir` it will be relative to `${CMAKE_CURRENT_BINARY_DIR}`. – Florian Jul 07 '15 at 19:21
6

My favorite option is to generate the executable in the right folder directly as explained here:

the secret is to use the target property RUNTIME_OUTPUT_DIRECTORY. This has per-configuration options (e.g. RUNTIME_OUTPUT_DIRECTORY_DEBUG).

set_target_properties(mylibrary PROPERTIES
                      RUNTIME_OUTPUT_DIRECTORY_DEBUG <debug path>
                      RUNTIME_OUTPUT_DIRECTORY_RELEASE <release path>
)

See this link for more information.

However you can run on your terminal also:

cmake --help-property "RUNTIME_OUTPUT_DIRECTORY"
cmake --help-property "RUNTIME_OUTPUT_DIRECTORY_<CONFIG>"

To copy the executable after the compilation is a good working solution.

Leos313
  • 5,152
  • 6
  • 40
  • 69
4

I use CMAKE_RUNTIME_OUTPUT_DIRECTORY. it will be the place where your output is going to compile. if you need to do another copy, then you will need to do tricks.

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_PATH}/bin/client/${CMAKE_BUILD_TYPE})