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

4

6

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.

Stinkhorn answered 7/7, 2015 at 19:2 Comment(0)
C
21

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

Chirpy answered 7/7, 2015 at 19:12 Comment(2)
Can you explain what I am supposed to fill in here?Stinkhorn
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) 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}.Chirpy
S
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.

Selry answered 29/10, 2019 at 10:47 Comment(1)
This is actually a really neat way of doing it!Payee
S
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})

Shammer answered 9/8, 2016 at 20:35 Comment(0)
C
0

EXECUTABLE_OUTPUT_PATH is what eventually worked for my purpose.

set(EXECUTABLE_OUTPUT_PATH "/my/output/dir/")
add_executable(foo foo.c)

Although the documentation for EXECUTABLE_OUTPUT_PATH says

Old executable location variable.

The target property RUNTIME_OUTPUT_DIRECTORY supersedes this variable for a target if it is set. Executable targets are otherwise placed in this directory.

setting RUNTIME_OUTPUT_DIRECTORY doesn't seem to have any effect for me, but setting EXECUTABLE_OUTPUT_PATH does.

I am using the latest release of cmake.

$ cmake --version
cmake version 3.28.1
Cheviot answered 29/1 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.