如何在Makefile中将参数传递给目标?

3

我想通过传递二进制代码来删除我的目录。 我的目录中有以下文件:

foo.c foo.o foo

我想要移除 foo.ofoo。现在我的 makefile 文件中有以下内容:

clean:
   rm *.o
   rm [...] #the [...] should be the argument

我想运行 make clean foo 命令,并且这里的 foo 将作为参数传递给 clean 目标。我曾经在某个地方读到,正确的方法应该是简单地在 makefile 中定义一个变量 $(BINARIES),然后在 rm 命令中使用该变量,但这样我每次想编译另一个二进制文件时都必须编辑该变量。所以,我想将二进制文件作为参数传递给 clean 目标。如何做到这一点?(另外,我不想像 make ARGS=foo clean 这样发出 make 命令,因为这太长了,我想用恰好三个单词:1)make,2)目标,3)参数。)

一个新手的错误:那些是 TAB 而不是空格吗? - Ted Lyngmo
1个回答

4

GNU Make 的简介是 make [OPTION]... [TARGET]...,因此我怀疑您唯一的选择就是

  1. finding or implementing some other make executable which implements the synopsis you want,

  2. use the make ARGS=foo clean syntax, or

  3. use a hack like pattern rules:

    clean-%:
        echo $(@:clean-%=%)
    

    Call it using make clean-foo, and it strips the "clean-" prefix and prints "foo".


1
请问您能否解释一下您最后的解决方案? - milanHrabos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接