cmake add_custom_command不运行
我正在尝试使用 add_custom_command 生成文件在构建过程中。该命令似乎从未运行,因此我制作了此测试文件。
cmake_minimum_required( VERSION 2.6 )
add_custom_command(
OUTPUT hello.txt
COMMAND touch hello.txt
DEPENDS hello.txt
)
我尝试跑步:
cmake .
make
Hello.txt并未生成。我做错了什么?
I'm trying to use add_custom_command to generate a file during the build. The command never seemed to be run, so I made this test file.
cmake_minimum_required( VERSION 2.6 )
add_custom_command(
OUTPUT hello.txt
COMMAND touch hello.txt
DEPENDS hello.txt
)
I tried running:
cmake .
make
And hello.txt was not generated. What have I done wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两个现有答案的问题是,它们要么使依赖关系全局(
add_custom_target(name all ...)
),要么将其分配给特定的单个文件(set_property(.. 。)
)如果您有许多需要它作为依赖关系的文件,它会令人讨厌。相反,我们想要的是一个目标,我们可以依赖另一个目标。这样做的方法是使用
add_custom_command
来定义规则,然后add_custom_target
根据该规则定义新目标。然后,您可以通过add_depentencies
将该目标添加为另一个目标的依赖关系。这种方法的优点:
some_target
并不是all
的依赖性,这意味着您仅在特定目标要求时才构建它。 (而add_custom_target(name all ...)
将无条件地为所有目标构建它。)some_target
是整个库的依赖性,因此它将在该库中的所有文件。这意味着,如果库中有很多文件,我们不必对其中每个文件进行set_property
。<代码> add_custom_command ,则只有在其输入更改时才会重建。 方法进行比较。
(将其与使用
add_custom_target(name as all ...)
在每个构建上运行的 ,请参阅此博客文章: https://samthursfield.wordpress.com/2015/11/21/cmake-depperencies-betecies-betweew-targets-and-targets-and-files-and-custom-commands/The problem with two existing answers is that they either make the dependency global (
add_custom_target(name ALL ...)
), or they assign it to a specific, single file (set_property(...)
) which gets obnoxious if you have many files that need it as a dependency. Instead what we want is a target that we can make a dependency of another target.The way to do this is to use
add_custom_command
to define the rule, and thenadd_custom_target
to define a new target based on that rule. Then you can add that target as a dependency of another target viaadd_dependencies
.The advantages of this approach:
some_target
is not a dependency forALL
, which means you only build it when it's required by a specific target. (Whereasadd_custom_target(name ALL ...)
would build it unconditionally for all targets.)some_target
is a dependency for the library as a whole, it will get built before all of the files in that library. That means that if there are many files in the library, we don't have to doset_property
on every single one of them.DEPENDS
toadd_custom_command
then it will only get rebuilt when its inputs change. (Compare this to the approach that usesadd_custom_target(name ALL ...)
where the command gets run on every build regardless of whether it needs to or not.)For more information on why things work this way, see this blog post: https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/
添加以下内容:
如果您熟悉makefiles,这意味着:
Add the following:
If you're familiar with makefiles, this means:
add_custom_target(运行全部...
解决方案将在您只有一个目标构建时对简单案例起作用,但是当您拥有多个顶级目标,例如应用程序和测试时会分解。我运行当我试图将一些测试数据文件打包到对象文件中时,我的单位测试将不取决于外部的任何内容。代码> set_property
。 您不必等待该命令(只有测试可执行的需求)才能完成。
仅构建应用程序目标, ()将使您的源树清洁生成的文件。
The
add_custom_target(run ALL ...
solution will work for simple cases when you only have one target you're building, but breaks down when you have multiple top level targets, e.g. app and tests.I ran into this same problem when I was trying to package up some test data files into an object file so my unit tests wouldn't depend on anything external. I solved it using
add_custom_command
and some additional dependency magic withset_property
.So now testData.cpp will generated before unit-tests.cpp is compiled, and any time testData.src changes. If the command you're calling is really slow you get the added bonus that when you build just the app target you won't have to wait around for that command (which only the tests executable needs) to finish.
It's not shown above, but careful application of
${PROJECT_BINARY_DIR}, ${PROJECT_SOURCE_DIR} and include_directories()
will keep your source tree clean of generated files.