Cmake: use add_custom_command to copy binary to sp

2019-01-24 09:06发布

I'm trying to copy all the binaries generated by cmake to a specific directory. I don't want to use EXECUTABLE_OUTPUT_PATH, keeping them in the building tree make development easier.

So I use add_custom_command to add a post build copy

# set the final binary dir
set(PROJECT_BINARY_DIR ${PROJECT_SOURCE_DIR}/bin)

# get name and location of binary, namecan change with os (cf. ".exe" suffix on windows)
GET_TARGET_PROPERTY(EXAMPLE_BIN_NAME example LOCATION)
# copy bin to binary folder
ADD_CUSTOM_COMMAND(TARGET example
          POST_BUILD
          COMMAND ${CMAKE_COMMAND} -E copy ${EXAMPLE_BIN_NAME} ${PROJECT_BINARY_DIR}/.
)

The problem is that at the first build the "bin" folder doesn't exist (the copy fail), but the "bin" folder is created just after. So at the second build the copy works.

Is there a way to make the custom command create the bin folder and then copy the binary ? Or is it possible to have two EXECUTABLE_OUTPUT_PATH with cmake ?

Thanks !

标签: cmake
1条回答
乱世女痞
2楼-- · 2019-01-24 09:23

Just make sure that the target location "bin" exists before invoking the copy command, i.e.:

ADD_CUSTOM_COMMAND(TARGET example
          POST_BUILD
          COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}
          COMMAND ${CMAKE_COMMAND} -E copy ${EXAMPLE_BIN_NAME} ${PROJECT_BINARY_DIR}/.
)
查看更多
登录 后发表回答