如何从命令行将参数传递给

如何从命令行将参数传递给

本文介绍了如何从命令行将参数传递给 Makefile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从命令行向 Makefile 传递参数?

How to pass argument to Makefile from command line?

我知道我能做到

$ make action VAR="value"
$ value

使用 Makefile

VAR = "default"
action:
    @echo $(VAR)

如何获得以下行为?

$ make action value
value

?

怎么样

$make action value1 value2
value1 value2

推荐答案

你可能不应该这样做;你打破了 Make 工作的基本模式.但它是:

You probably shouldn't do this; you're breaking the basic pattern of how Make works. But here it is:

action:
        @echo action $(filter-out $@,$(MAKECMDGOALS))

%:      # thanks to chakrit
    @:    # thanks to William Pursell


解释第一个命令,


To explain the first command,

$(MAKECMDGOALS) 是在命令行上拼出的目标"列表,例如动作值1值2".

$(MAKECMDGOALS) is the list of "targets" spelled out on the command line, e.g. "action value1 value2".

$@ 是一个 表示规则目标的名称,在本例中为action".

$@ is an automatic variable for the name of the target of the rule, in this case "action".

filter-out 是一个从列表中删除一些元素的函数.所以 $(filter-out bar, foo bar baz) 返回 foo baz(它可以更微妙,但我们不需要这里的微妙).

filter-out is a function that removes some elements from a list. So $(filter-out bar, foo bar baz) returns foo baz (it can be more subtle, but we don't need subtlety here).

将这些放在一起,$(filter-out $@,$(MAKECMDGOALS)) 返回命令行上指定的目标列表,而不是action",可能是value1 value2".

Put these together and $(filter-out $@,$(MAKECMDGOALS)) returns the list of targets specified on the command line other than "action", which might be "value1 value2".

这篇关于如何从命令行将参数传递给 Makefile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:11