【问题标题】:Can make "target" take an argument?可以让“目标”接受论证吗?
【发布时间】:2011-07-30 06:20:42
【问题描述】:

可以说我正在做 mk "target" 来构建一些东西。是否可以将参数传递给它? 即 mk "target" "x" 它会做相应的事情吗?我知道我将提供 mk "target" 一个参数,并且我知道它的语义,只是事先不知道名称。

可能吗?

【问题讨论】:

标签: c makefile


【解决方案1】:

你可能想使用GNU Make's "Variables"

$ cat Makefile

ifndef LOLCAKES
   LOLCAKES=1
endif

all:
   @echo $(LOLCAKES)

$ make all LOLCAKES=42

你没有解释你想要完成什么,所以很难知道你在追求什么样的“争论”。

【讨论】:

  • 非常感谢托马拉克。基于作为字符串的参数,我的“mk 目标”将查看不同的路径来进行制作。因此,字符串参数基本上是我向用户询问他/她想要查看的目录的一种方式。这可以解释吗?再次感谢。
  • @hari:听起来这就是你的解决方案。
  • 对不起,我对make的了解很少,但你的意思是,应该要求用户导出一个环境变量并使用它吗?我不太明白你的意思: echo $(LOLCAKES) under target all:
  • @hari:不,我没有使用环境变量。阅读手册中关于make's variables 的部分。
  • 谢谢托马拉克。所以我可以这样做:mk target name=x 并让我的 makefile 在任何地方都使用“name”。
【解决方案2】:

make target x 将导致make 尝试构建target x。没有办法像您期望的那样拥有修饰符。一个好的解决方案是使用复合名称的规则:

target: target.debug target.release

target.release:
    # recipe for release build

target.debug:
    # recipe for debug build

然后您可以使用target.debugtarget.release 或仅使用target,并获得一些理智的行为。

【讨论】:

    【解决方案3】:

    你可以使用环境变量:

    $ cat Makefile 
    all:
        @echo $(FOO)
    $ FOO=bar make
    bar
    

    【讨论】:

      猜你喜欢
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多