Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically copy executable to directory after build in CLion? (using CMake)

Tags:

build

cmake

clion

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.

like image 887
becko Avatar asked Nov 29 '22 07:11

becko


2 Answers

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

like image 52
Florian Avatar answered Apr 29 '23 19:04

Florian


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.

like image 39
Leos313 Avatar answered Apr 29 '23 18:04

Leos313