为什么CMake区分“目标”和“命令”? [英] Why does CMake make a distinction between a "target" and a "command"?

查看:151
本文介绍了为什么CMake区分“目标”和“命令”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CMake语义中,目标和命令之间有一些区别,这是令人困惑的。在Makefiles中,没有这样的区别:

In CMake semantics there is some sort of distinction between "targets" and commands" that is baffling me. In Makefiles, there is no such distinction:

targetname:dependency
    command

文件。

在CMake中,你有类似add_custom_command和add_custom_target这样的命令有重叠的功能,甚至在官方文档中语义混乱,即在Mastering CMake,第5版,第110页添加自定义目标下:

In CMake you have commands like "add_custom_command" and "add_custom_target" that have overlapping functionality, and even in the official documentation the semantics are confused, i.e. in "Mastering CMake, 5th edition", page 110 under "Adding a custom target":


DEPENDS参数设置了自定义目标
和自定义命令。

The DEPENDS argument sets up a dependency between the custom target and the custom commands.

我的理解是目标(生成的文件)生成或否),以及一个实际执行生成的命令,一个目标取决于命令是无意义的。更糟糕的是,有两种风格的add_custom_command将附加命令附加到现有目标,

My understanding is that targets (generated files) have dependencies (other files, generated or no), and a command to actually do the generation. It is nonsensical to say a target depends on a command. To make matters worse, there are two flavors of "add_custom_command" that either attach an additional command to an existing target, or spit the command out into the ether.

有人可以解释为什么这种区别甚至存在吗?

Can someone please explain why this distinction even exists?

推荐答案

目标



一般来说,目标包括可执行文件或库是通过调用 add_executable add_library ,并且可以设置多个属性

它们可以有依赖关系,对于这些目标,只是意味着依赖关系将在它们的依赖关系之后构建。

They can have dependencies on one another, which for targets such as these just means that dependent ones will be built after their dependencies.

但是,您也可以通过 <$ c $定义自定义定位 c> add_custom_target 。从文档:

However, you can also define "custom targets" via add_custom_target. From the docs:


添加具有执行给定命令的给定名称的目标。目标没有输出文件,并且总是考虑到日期,即使命令尝试使用目标的名称创建文件。使用ADD_CUSTOM_COMMAND生成具有依赖关系的文件。默认情况下,不取决于自定义目标。使用ADD_DEPENDENCIES向其他目标添加依赖项。

Adds a target with the given name that executes the given commands. The target has no output file and is ALWAYS CONSIDERED OUT OF DATE even if the commands try to create a file with the name of the target. Use ADD_CUSTOM_COMMAND to generate a file with dependencies. By default nothing depends on the custom target. Use ADD_DEPENDENCIES to add dependencies to or from other targets.

因此,这些与正常目标不同,这将产生一个exe或lib,但他们仍然受益于目标可以拥有的所有属性,包括具有或依赖。它们显示为可以构建的目标(例如 make MyCustomTarget msbuild MyCustomTarget.vcxproj )。当你构建它们时,你只是调用为他们设置的命令。如果他们依赖于其他目标(正常或自定义),那么这些将首先构建。

So these are different from "normal" targets in that they don't represent things which will produce an exe or lib, but they still benefit from all the properties that targets can have, including having or being dependencies. They appear as a target which can be built (e.g. make MyCustomTarget or msbuild MyCustomTarget.vcxproj). When you build them, you're simply invoking the commands that have been set for them. If they have dependencies on other targets (normal or custom), then these will be built first.


通过 add_custom_command 是非常不同的,它不是一个可构建对象,并没有可设置的属性, target does - 它不是一个命名对象,可以在添加到CMakeLists.txt后再次显式引用。

A custom command defined via add_custom_command is quite different in that it's not a "buildable" object, and doesn't have settable properties in the way that a target does - it's not a named object which can be explicitly referred to again after it's added in the CMakeLists.txt.

它基本上是一个命令(或一组命令)这将在构建一个依赖目标之前被调用。这是所有的依赖真正意味着这里(至少是我的看法) - 它只是说如果A依赖B,那么B将构建/执行之前A被建立。

It is basically a command (or set of commands) which will be invoked before building a dependent target. That's all that "depends" really means here (at least that's how I view it) - it's just saying that if A depends on B, then B will be built/executed before A is built.

可以使用 add_custom_command(TARGET target ... )窗体显式设置自定义命令的依赖者,也可以通过创建包含文件的目标通过 add_custom_command(OUTPUT output1 ... 形式)生成。

The dependees of a custom command can be either set explicitly using the add_custom_command(TARGET target ... form, or implicitly by creating targets which include the files generated via the add_custom_command(OUTPUT output1 ... form.

在第一种情况下,

在第二种情况下,这更复杂一些,如果自定义命令命令具有依赖于它的输出文件(和输出文件不存在)的目标,它在构建这些依赖对象之前被调用,当你执行例如 add_library(MyLib output1 .h ...)其中 output1.h 是通过 add_custom_command(OUTPUT output1.h ... )

In the second case, it's a little more complex. If the custom command has targets which depend on its output file (and the output file doesn't already exist), it is invoked before these dependent objects are built. The dependencies are implicitly created when you do e.g. add_library(MyLib output1.h ... ) where output1.h is a file generated via add_custom_command(OUTPUT output1.h ... ).

这篇关于为什么CMake区分“目标”和“命令”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆