With cmake how can I use a target created in a subdirectory as the target for add_custom_command?

25 Views Asked by At

An example is probably the best way to show what I mean. Suppose I have in my top-level CMakeLists.txt file:

cmake_minimum_required(VERSION 3.26)
project(TopLevel)

add_subdirectory(my_exe)

add_custom_command(
    TARGET my_exe
    POST_BUILD
    COMMAND ${CMAKE_COMMAND}
    ARGS -E copy $<TARGET_FILE:my_exe> /tmp/foo
)

And in /my_exe/CMakeLists.txt:

project(my_exe)
add_executable(${PROJECT_NAME} main.cpp)

cmake gives the following error:

CMake Error at CMakeLists.txt:6 (add_custom_command):
  TARGET 'my_exe' was not created in this directory.

How can I get this custom command to work given that it depends on something built in a subdirectory?

0

There are 0 best solutions below