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.
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.
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
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 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.
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})
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
© 2022 - 2024 — McMap. All rights reserved.