【问题标题】:CMake: write executables to multiple subdirectoriesCMake:将可执行文件写入多个子目录
【发布时间】:2019-03-15 20:32:21
【问题描述】:

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

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

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

我希望将大部分我的可执行文件写入build/bin 目录,而那些从测试目录下的源代码构建的可执行文件写入build/bin/tests 目录。

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

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

理想情况下,我希望能够在 tests 目录中的 CMakeLists.txt 文件中设置一些内容,并将其滴入子目录。

【问题讨论】:

  • "我还配置了 CMake 以将可执行文件写入build/bin 目录。" - 你是怎么做到的?通常,类似的方式应该适合将测试放入build/bin/tests 目录。变量CMAKE_RUNTIME_OUTPUT_DIRECTORY 和属性RUNTIME_OUTPUT_DIRECTORY 都应该可以工作。
  • @Tsyvarev:我通过在根级 CMakeList.txt 文件中调用 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 将我的可执行文件放置在 build/bin 中。

标签: cmake


【解决方案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)

测试/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 变量的当前 值。这个事实可以在documentation关于RUNTIME_OUTPUT_DIRECTORY的目标属性中找到,该属性受变量影响。

【讨论】:

  • 好吧,我以为我已经尝试过了,但显然我搞砸了。这正是我所需要的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2014-08-26
  • 2013-01-19
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 2012-12-27
相关资源
最近更新 更多