我很长时间都想了解 生成器表达式,例如 CMake 中的 $
,何时以及如何使用它们.任何人都可以用一些例子清楚地解释它.提前致谢
I'm trying for long time to understand the benefit of generator expressions such as $<xxx:yy>
in CMake, when and how to use them.
Can anybody explain it clearly with some examples.
Many thank in advance
CMake 首先会解析您项目中的 CMakeLists.txt
文件 - 名为配置阶段" - 然后生成您的构建环境- 命名为生成阶段".
CMake does first parse the CMakeLists.txt
files in your project - named "Configuration Phase" - and then it generates your build environment - named "Generation Phase".
所以基本上生成器表达式适用于只有生成器知道的所有内容:
So basically the generator expressions are for everything only the generator could know:
以下是我在项目中使用生成器表达式的示例:
Here are examples where I use generator expressions in my project:
复制可执行文件旁边的文件(在多配置环境中,您不能只使用像 CMAKE_CURRENT_BINARY_DIR
这样的变量)
add_custom_command(
TARGET library1
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"$<TARGET_FILE:library1>"
"$<TARGET_FILE_DIR:mainProject>/$<TARGET_FILE_NAME:library1>"
)
CMake post-build-event:复制编译的库
add_custom_command(
TARGET myBinary
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/myTest.txt"
"$<TARGET_FILE_DIR:myBinary>/myTest.txt"
)
如何添加外部测试文件到一个 cmake 项目
区分例如DEBUG
或 RELEASE
配置
add_compile_options("$<$<CONFIG:DEBUG>:/MDd>")
对于Cmake,你能用`add_compiler_flags()`命令修改发布/调试编译器标志吗?
设置编译器标志的现代方法跨平台cmake项目
使用 TARGET_PROPERTY
生成器表达式,您可以做很多事情,例如
With the TARGET_PROPERTY
generator expression you could do a lot of things e.g.
file(GENERATE
OUTPUT "includes.txt"
CONTENT "$<TARGET_PROPERTY:motor,INCLUDE_DIRECTORIES>
"
)
CMake 没有选择链接的 INTERFACE_INCLUDE_DIRECTORIES图书馆
这篇关于Cmake 生成器表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!