为所有目标做些事情 [英] Do something for all targets
问题描述
对所有(二进制)目标进行附加处理的最佳方法是什么?
What is the best way to do additional stuff for all (binary) targets?
示例:
- 我要检查每个库名称是否遵循模式。
- 我要对每个可执行文件签名。
我不希望C / C ++开发人员使用非标准命令(例如 add_library2
)。我希望他们使用并学习CMake的官方功能,但要让他们做其他的,特定于项目的工作。
I dont what the C/C++ developers to use nonstandard commands (like add_library2
). I want them to use and learn the official CMake functions, but have them do additonal, project specific, stuff.
推荐答案
-in CMake函数 add_library
和 add_executable
可以通过定义相同名称的CMake函数来覆盖。例如,要自动签署所有添加的可执行文件,请添加以下代码:
The built-in CMake functions add_library
and add_executable
can be overidden by defining CMake functions of the same name. E.g., to automatically sign all added executables add the following code:
function (add_executable _name)
_add_executable(${ARGV})
if (TARGET ${_name})
add_custom_command(TARGET ${_name} POST_BUILD
COMMAND sign_executable $<TARGET_FILE:${_name}>)
endif()
endfunction()
原始内置 add_executable
可以通过在其下划线添加前缀来调用。可以将相同的模式应用于 add_library
来检查库的名称。
The original built-in add_executable
can be invoked by prefixing it with an underscore character. The same pattern can be applied to add_library
to check the name of the library.
这篇关于为所有目标做些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!