cmake add_custom_command不运行

发布于 2025-01-27 01:01:15 字数 435 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫性小仙女 2025-02-03 01:01:16

两个现有答案的问题是,它们要么使依赖关系全局(add_custom_target(name all ...)),要么将其分配给特定的单个文件(set_property(.. 。))如果您有许多需要它作为依赖关系的文件,它会令人讨厌。相反,我们想要的是一个目标,我们可以依赖另一个目标。

这样做的方法是使用add_custom_command来定义规则,然后add_custom_target根据该规则定义新目标。然后,您可以通过add_depentencies将该目标添加为另一个目标的依赖关系。

# this defines the build rule for some_file
add_custom_command(
  OUTPUT some_file
  COMMAND ...
)
# create a target that includes some_file, this gives us a name that we can use later
add_custom_target(
  some_target
  DEPENDS some_file
)
# then let's suppose we're creating a library
add_library(some_library some_other_file.c)
# we can add the target as a dependency, and it will affect only this library
add_dependencies(some_library some_target)

这种方法的优点:

  • 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 then add_custom_target to define a new target based on that rule. Then you can add that target as a dependency of another target via add_dependencies.

# this defines the build rule for some_file
add_custom_command(
  OUTPUT some_file
  COMMAND ...
)
# create a target that includes some_file, this gives us a name that we can use later
add_custom_target(
  some_target
  DEPENDS some_file
)
# then let's suppose we're creating a library
add_library(some_library some_other_file.c)
# we can add the target as a dependency, and it will affect only this library
add_dependencies(some_library some_target)

The advantages of this approach:

  • some_target is not a dependency for ALL, which means you only build it when it's required by a specific target. (Whereas add_custom_target(name ALL ...) would build it unconditionally for all targets.)
  • Because 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 do set_property on every single one of them.
  • If we add DEPENDS to add_custom_command then it will only get rebuilt when its inputs change. (Compare this to the approach that uses add_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/

小嗲 2025-02-03 01:01:16

添加以下内容:

add_custom_target(run ALL
    DEPENDS hello.txt)

如果您熟悉makefiles,这意味着:

all: run
run: hello.txt

Add the following:

add_custom_target(run ALL
    DEPENDS hello.txt)

If you're familiar with makefiles, this means:

all: run
run: hello.txt
抚笙 2025-02-03 01:01:15

add_custom_target(运行全部...解决方案将在您只有一个目标构建时对简单案例起作用,但是当您拥有多个顶级目标,例如应用程序和测试时会分解。

我运行当我试图将一些测试数据文件打包到对象文件中时,我的单位测试将不取决于外部的任何内容。代码> set_property

add_custom_command(
  OUTPUT testData.cpp
  COMMAND reswrap 
  ARGS    testData.src > testData.cpp
  DEPENDS testData.src 
)
set_property(SOURCE unit-tests.cpp APPEND PROPERTY OBJECT_DEPENDS testData.cpp)

add_executable(app main.cpp)
add_executable(tests unit-tests.cpp)

。 您不必等待该命令(只有测试可执行的需求)才能完成。

仅构建应用程序目标, ()将使您的源树清洁生成的文件。

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 with set_property.

add_custom_command(
  OUTPUT testData.cpp
  COMMAND reswrap 
  ARGS    testData.src > testData.cpp
  DEPENDS testData.src 
)
set_property(SOURCE unit-tests.cpp APPEND PROPERTY OBJECT_DEPENDS testData.cpp)

add_executable(app main.cpp)
add_executable(tests unit-tests.cpp)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文