收录日期:2020/06/02 03:48:23 时间:2009-08-07 16:05:41 标签:recursion,makefile,gnu-make

I need to write a quick makefile for building all my projects. This is C++ code and I am using gmake.

Say I have a list of directories, I want to cd to each, issue gmake command and if it succeeds, go to the next one and so on.

I cooked this by looking at the gmake manual

.PHONY: all clean dirs $(DIRS)

dirs: $(DIRS)

$(DIRS): \n\t
    $(MAKE) -C $@

It works for the "all" target - if I just type gmake, it does the right thing. But if I do gmake clean it does nothing.

I am learning gmake as I go, so I am certainly doing something silly here :)

Thanks for any help.

In order to recursively make something other than the first target (I'm guessing all is your first target), you need to give the sub-make an idea of what to build. You can do so with the MAKEFLAGS and MAKECMDGOALS variables.

For example:

$(DIRS):
        $(MAKE) -C "$@" $(MAKEFLAGS) $(MAKECMDGOALS)

Your rule was not passing along the target names, e.g. clean, so the sub-make had no work to do (since all was already built).