CMake add_custom_command 未运行

发布于 2024-09-03 15:08:22 字数 408 浏览 6 评论 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

九八野马 2024-09-10 15:08:22

当您只有一个正在构建的目标时,add_custom_target(run ALL ...) 解决方案适用于简单的情况,但当您有多个顶级目标(例如应用程序和测试)时,该解决方案就会崩溃。

我跑了当我尝试将一些测试数据文件打包到目标文件中时,我遇到了同样的问题,这样我的单元测试就不会依赖于任何外部内容,我使用 add_custom_command 和一些额外的依赖魔术来解决它。 code>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)

因此,现在 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 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.

白色秋天 2024-09-10 15:08:22

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

执行此操作的方法是使用 add_custom_command 定义规则,然后使用 add_custom_target 基于该规则定义新目标。然后,您可以通过 add_dependency 将该目标添加为另一个目标的依赖项。

# 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 操作。
  • 如果我们将 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 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/

溺深海 2024-09-10 15:08:22

添加以下内容:

add_custom_target(run ALL
    DEPENDS hello.txt)

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

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