Getting a cmake executable path to run it on a cus

2019-03-05 14:56发布

I am creating an executable using the add_executable(foo sources.cpp) then I would like to have a target that runs foo, so right now I'm doing this:

add_custom_target(run_foo 
   COMMAND ${CMAKE_BINARY_DIR}/test/foo 
   DEPENDS foo 
   WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

it works now, but I feel like I'm doing it wrong by hardcoding the path to the binary in "COMMAND". Isn't there a way to get the path to the binary from foo ?

标签: c++ cmake
2条回答
叼着烟拽天下
2楼-- · 2019-03-05 15:05
爷的心禁止访问
3楼-- · 2019-03-05 15:11

In fact, you don't even need a generator expression :) At least /w modern CMake:

If COMMAND specifies an executable target name (created by the add_executable() command) it will automatically be replaced by the location of the executable created at build time.

add_executable(foo ...)
add_custom_target(COMMAND foo ...)
查看更多
登录 后发表回答