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(run ALL ...
) 解决方案适用于简单的情况,但当您有多个顶级目标(例如应用程序和测试)时,该解决方案就会崩溃。我跑了当我尝试将一些测试数据文件打包到目标文件中时,我遇到了同样的问题,这样我的单元测试就不会依赖于任何外部内容,我使用
add_custom_command
和一些额外的依赖魔术来解决它。 code>set_property。因此,现在 testData.cpp 将在编译 unit-tests.cpp 之前生成,并且只要 testData.src 发生变化,如果您调用的命令非常慢,您就会得到额外的好处。只构建应用程序目标,您不必等待该命令(只有测试可执行文件需要)完成,
上面没有显示,但要小心应用
${PROJECT_BINARY_DIR}、${PROJECT_SOURCE_DIR} 和 include_directories。 ()
将使您的源代码树中没有生成的文件。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.两个现有答案的问题在于,它们要么使依赖项全局化(
add_custom_target(name ALL ...)
),要么将其分配给特定的单个文件(set_property(.. .)
) 如果您有许多文件需要它作为依赖项,这会变得令人讨厌。相反,我们想要的是一个可以依赖另一个目标的目标。执行此操作的方法是使用
add_custom_command
定义规则,然后使用add_custom_target
基于该规则定义新目标。然后,您可以通过add_dependency
将该目标添加为另一个目标的依赖项。这种方法的优点:
some_target
不是ALL
的依赖项,这意味着您仅在特定目标需要时才构建它。 (而add_custom_target(name ALL ...)
将为所有目标无条件地构建它。)some_target
是整个库的依赖项,所以它将在之前构建该库中的所有文件。这意味着如果库中有很多文件,我们不必对每个文件都执行set_property
操作。DEPENDS
添加到add_custom_command
中,那么只有当其输入发生变化时,它才会被重建。 (将此与使用add_custom_target(name ALL ...)
的方法进行比较,其中命令在每个构建上运行,无论是否需要。)有关为什么会这样工作的更多信息,请参阅此博客文章: https://samthursfield.wordpress.com/2015/11/21/cmake-dependency- Between-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/
添加以下内容:
如果您熟悉 makefile,这意味着:
Add the following:
If you're familiar with makefiles, this means: