gpt4 book ai didi

linux - cmake 2.8自定义目标复制多个文件

转载 作者:行者123 更新时间:2023-12-02 19:48:56 25 4
gpt4 key购买 nike

我不得不在 Linux 环境中使用旧版 cmake 2.8.12。

作为预构建步骤,我必须将多个头文件从源目录复制到目标目录。我决定使用 add_custom_target 子句。如果这本身就是个坏主意,请告诉我。例如:

add_custom_target( prebuild
COMMENT "Prebuild step: copy other headers"
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/../other/include/alpha.h ${CMAKE_SOURCE_DIR}/include/other
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/../other/include/bravo.h ${CMAKE_SOURCE_DIR}/include/other
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/../other/include/charlie.h ${CMAKE_SOURCE_DIR}/include/other
)

add_executable( myapp main.cxx )

# My application depends on the pre-build step.
add_dependencies( myapp prebuild )

set_target_properties( myapp PROPERTIES COMPILE_FLAGS "-g" )
install( TARGETS myapp DESTINATION ${BIN_INSTALL_DIR} )

列出每个头文件会很乏味。我知道如何搜索所有头文件并将它们放入列表变量中。例如。

file( GLOB other_headers "${CMAKE_SOURCE_DIR}/../other/include/*.h" )

但是,如何将该列表变量放入 add_custom_target 子句中使用?

有没有办法在 add_custom_target 子句中复制多个文件?

有没有更好的方法来复制多个文件作为预构建步骤,这些文件可以成为我的应用程序构建的依赖项?

受限于旧版本的 cmake 限制了我的选择。以下是我尝试过但没有成功的事情。

  • 如果我有 cmake 版本 3.5,则 copy_if_different 命令可以采用多个源路径。
  • COMMAND_EXPAND_LISTS 添加到 add_custom_target 子句无效。在更新的 cmake 版本之前,它必须不可用。
  • add_custom_target 子句中使用 foreach 循环不起作用。在我看到的其他示例中,他们总是使用 foreach 循环,并且 add_custom_target 位于该循环内。但是,如果我这样做,那么我不知道如何使它成为我的应用程序构建的依赖项。

最佳答案

Using a foreach loop within the add_custom_target clause does not work.

但是使用 foreach 您可以创建一个包含所有必需命令的变量。然后在 add_custom_target 中使用该变量:

set(commands)

# Assume 'other_headers' contain list of files
foreach(header ${other_headers})
list(APPEND commands
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${header} ${CMAKE_SOURCE_DIR}/include/other)
endforeach()

add_custom_target( prebuild
COMMENT "Prebuild step: copy other headers"
${commands}
)

关于linux - cmake 2.8自定义目标复制多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58701352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com