问题描述
在GNU make
中,如果目标A依赖于两个目标B和C,但是使用目标C来构建A则要求已经构建了目标B(但是目标C本身并不依赖于B),我可以使用仅订购的先决条件.
In GNU make
, if a target A depend on two targets B and C, but using target C to build A requires that target B has already been built (target C itself doesn't depend on B however), I can use order-only prerequisites.
CMake
中是否有其他选择?我知道CMake
只是一个配置工具,所以这个问题可能会更好地表述为我可以用某种方式编写CMakeLists.txt以便它生成仅具有先决条件的Makefile吗?"
Are there any alternatives in CMake
? I know CMake
is just a configuration tool so the question may be better rephrased as "Can I write CMakeLists.txt in a way so that it generates Makefiles with order-only prerequisites?"
也欢迎其他解决方案.任何帮助将不胜感激.
Alternative solutions are also welcome. Any help will be appreciated.
推荐答案
使用仅订购先决条件的典型情况是,在将目录用于某些make规则之前,请确保已准备好目录.通常, cmake 会处理它所控制的所有目标的目录.但是,如果您确实需要某个目录,则可以使用以下自定义目标命令:
The typical case of using an order-only prerequisite is to make sure a directory is prepared before it is used in some make rule. Usually, cmake takes care of the directories for all the targets that are under it's control. But if you really need some directory, you can use a custom target command like this:
add_custom_target(build-time-make-directory ALL
COMMAND ${CMAKE_COMMAND} -E make_directory ${directory})
此目标是无输出",因此将始终进行构建,但是不一定要重建依赖于此目标的目标.
This target is "with no output" so it will always be built, but the targets that depend on it won't necessarily be rebuilt.
这不限于mkdir
.这是另一个示例:
This is not limited to mkdir
. Here is another example:
add_executable(qqexe qq.c)
add_custom_target(_custom_target COMMAND touch pp.c COMMENT pp)
add_dependencies(qqexe _custom_target)
每次运行 make 时, pp.c 的时间戳都会刷新,但是 qqexe 不会重建,除非 qq.c 更改.
Each time you run make, the timestamp of pp.c will refresh, but the qqexe will not be rebuilt unless the timestamp of qq.c changes.
这篇关于CMake中是否提供了GNU Makefile中仅订购的先决条件,如果没有,替代选项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!