$$ @和Makefile中的管道符号代表什么? [英] What does $$@ and the pipe symbol in Makefile stand for?

查看:337
本文介绍了$$ @和Makefile中的管道符号代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下Makefile定义中:

In the following Makefile definition:

  1. 倒数第二行的$$@代表什么?
  2. 中线的|符号怎么办?
  1. What does the $$@ in the second last line stand for?
  2. What about the | symbol in the middle line?

define KERNEL_RULE
$(DESTDIR)/$(1) : kernel_modules
$(DEST_DIR)/$(1) : $(DESTDIR)/$(1) | $(DEST_DIR)
    cp $(DESTDIR)/$(1) $$@
endef

推荐答案

(您有很多可悲的变量名选择;让我们将DESTDIR更改为SOURCE_DIR并让DEST_DIR不理.)

(You have a lamentable choice of variable names; let's change DESTDIR to SOURCE_DIR and leave DEST_DIR alone.)

假设您正在编写一条普通规则:

Suppose you were writing an ordinary rule:

$(DEST_DIR)/foo : $(SOURCE_DIR)/foo
    cp $(SOURCE_DIR)/foo $(DEST_DIR)/foo

可以,但是冗余很麻烦.迟早您会在前提条件中更改$(DEST_DIR)/foo,但忘记在规则中进行更改.而且该规则很难阅读.因此,我们放入了自动变量:

That works, but the redundancy is troublesome. Sooner or later you'll change $(DEST_DIR)/foo in the preq but forget to change it in the rule. And the rule is hard to read. So we put in an automatic variable:

$(DEST_DIR)/foo : $(SOURCE_DIR)/foo
    cp $(SOURCE_DIR)/foo $@

此规则运行时,$@将扩展为目标名称$(DEST_DIR)/foo. (我们甚至可以做得更好,但让我们停在那里.)

When this rule runs, $@ will be expanded to the name of the target, $(DEST_DIR)/foo. (We can do even better than that, but let's stop there.)

现在,我们要确保$(DEST_DIR)存在,然后再执行此规则,但我们不完全希望 是先决条件,因为缺少该目录是不够的使该规则运行.因此,我们将其设为仅订购的先决条件:

Now we want to make sure that $(DEST_DIR) exists before this rule runs, but we don't want it to be a prerequisite exactly, because the absence of that directory shouldn't be enough to cause this rule to run. So we make it an order-only prerequisite:

$(DEST_DIR)/foo : $(SOURCE_DIR)/foo | $(DEST_DIR)
    cp $(SOURCE_DIR)/foo $@

现在,我们需要针对不同目标的许多这样的规则,而不是这样做聪明的方法,我们将使用,一种用于动态创建规则的模板.

Now we want many rules like this, for different targets, and instead of doing it the smart way, we'll use a "canned recipe", sort of a template for creating rules on the fly.

# This won't work
define KERNEL_RULE
$(SOURCE_DIR)/$(1) : kernel_modules
$(DEST_DIR)/$(1) : $(SOURCE_DIR)/$(1) | $(DEST_DIR)
    cp $(SOURCE_DIR)/$(1) $@
endef

问题在于,当我们评估此定义时,$@将被扩展,并且由于尚未成为规则,因此将扩展为空.所以我们将其更改为$$@:

The problem is that when we evaluate this definition, $@ will be expanded, and since it isn't a rule yet, it will expand to nothing. So we change it to $$@:

# This will work
define KERNEL_RULE
$(SOURCE_DIR)/$(1) : kernel_modules
$(DEST_DIR)/$(1) : $(SOURCE_DIR)/$(1) | $(DEST_DIR)
    cp $(SOURCE_DIR)/$(1) $$@
endef

当Make调用此定义时,$$@会扩展为$@,然后,如果/在运行规则时,$@会扩展为目标名称.

When Make calls this definition, $$@ expands to $@, then if/when it runs the rule, $@ will expand to the name of the target.

这篇关于$$ @和Makefile中的管道符号代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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