CMake:将可执行文件写入多个子目录

时间:2019-03-15 20:32:22

标签: cmake

我有一个看起来像这样的源代码和构建树:

+-build/
  +-bin/
+-modules/
  +-src/
  +-tests/
    +-test1/
    +-test2/

我还配置了CMake将可执行文件写入build/bin目录。一切正常。

我希望将大部分可执行文件写入build/bin目录,而将根据测试目录下的源代码生成的可执行文件写入build/bin/tests目录

这可能吗?有人可以指出我正确的方向吗?

我尝试直接设置RUNTIME_OUTPUT_DIRECTORY变量,并尝试使用set_target_properties,但没有成功。

理想情况下,我希望能够在tests目录的CMakeLists.txt文件中进行设置,并将其下放到子目录中。

1 个答案:

答案 0 :(得分:1)

您可以根据需要将CMAKE_RUNTIME_OUTPUT_DIRECTORY变量设置为很多次。每个设置只会影响自该设置以来创建的可执行文件,直到下一个可执行文件。

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Futher executables will be placed under 'bin/'
add_executable(my_program <sources>...)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/tests)
# Futher executables will be placed under 'bin/tests/' instead
add_executable(test1 <sources>...)
add_executable(test2 <sources>...)

如果要在单独 CMakeLists.txt中创建测试,则可以将设置模块化:

CMakeLists.txt

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Futher executables will be placed under 'bin/'
add_executable(my_program <sources>...)

add_subdirectory(tests)

tests / CMakeLists.txt

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/tests)
# Executables created below will be placed under 'bin/tests/' instead
add_executable(test1 <sources>...)
add_executable(test2 <sources>...)

实际上,当您调用add_executable时,它仅使用CMAKE_RUNTIME_OUTPUT_DIRECTORY变量的 current 值。这个事实可以在documentation关于RUNTIME_OUTPUT_DIRECTORY目标属性中找到,该属性受变量影响。

相关问题