如何从命令行传递参数的Makefile? [英] How to pass argument to Makefile from command line?

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

问题描述

如何从命令行参数传递给Makefile文件?

我知道我能做到

  $做出行动VAR =值
$值

的Makefile

  VAR =默认
行动:
    @echo $(VAR)

我如何获得以下行为?

  $做出动作值

关于

如何

  $做出动作值1值2
值1 VALUE2


解决方案

您可能不应该这样做;你打破的是如何使作品的基本格局。但在这里,它是:

 的行动:
        行动@echo $(过滤出$ @ $(MAKECMDGOALS))%:#感谢chakrit
    @:#感谢威廉Pursell

编辑:的结果
为了解释的第一个命令,

$(MAKECMDGOALS)是在命令行上阐述了目标,例如列表行动值1 VALUE2。

$ @ 是一个自动变量,在规则的目标的名称,在这种情况下,行动。

过滤出是一个函数,从列表中移除一些元素。因此, $(过滤出来吧,富酒吧巴兹)收益富巴兹(也可以是更微妙的,但我们这里不需要微妙之处)。

把这些结合在一起,并 $(过滤出$ @ $(MAKECMDGOALS))返回的不是行动等命令行上指定的目标名单,这可能是值1 VALUE2。

How to pass argument to Makefile from command line?

I understand I can do

$ make action VAR="value"
$ value

with Makefile

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

How do I get the following behavior?

$ make action value
value

?

How about

$make action value1 value2
value1 value2

解决方案

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

EDIT:
To explain the first command,

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

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

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).

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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