How to pass argument to target in makefile?

Viewed 1072

I would like to clean my dir by passing the binary to delete. In my directory are those files:

foo.c foo.o foo

I would like to remove foo.o and foo. Now in my makefile, I have this:

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

So I would like to run make clean foo, and the foo here would be the argument passed to the clean target. I have read somewhere that the proper way should be to simply make vairable $(BINARIES) inside the makefile, and use that variable in rm command, but then I would have to edit the variable every time I would like to compile another binary. So instead, I want to pass that binary as argument to the clean target. How to do that? (And Also, I do not want to issue the make like make ARGS=foo clean, this is too long, I want exactly 3 words. 1) make, 2) target, 3)) argument.

1 Answers

The synopsis for GNU Make is make [OPTION]... [TARGET]..., so I suspect your only options are

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

Related