Makefile: why always use `$(MAKE)` instead of `make`?

假如想象 提交于 2020-01-03 05:23:12

问题


I usually use a high level build system like cmake for building my C/C++ code. But for various reasons I am using straight GNU make.

I am doing recursive build where each directory has a makefile.

I recently had to change all my makefiles from using make to $(MAKE) and then parallel builds started working.

The literature I've read all say "always use $(MAKE)" (O'Reilly book, 5+ solutions on stackoverflow.com from people with greater than 10-40K points).

In your makefiles, why always use $(MAKE) instead of make?

(obviously ONE reason is because $(MAKE) somehow enables parallel builds.)

Is there more happening than simple substitution of $(MAKE) with make?


回答1:


Matthieu's answer is correct, but not complete.

The best thing to do is read the GNU make manual section related to using the MAKE variable.

In addition to using the correct make executable (which is reason enough), GNU make attempts to understand which recipes invoke recursive instances of make. Any recipe that does so is treated specially in a few ways:

First, that recipe is invoked even if you run make -n, make -q, or make -t.

Second, in order to communicate with its sub-makes (for example, to allow the jobserver to work properly) make maintains a set of open file descriptors (for pipes). For "normal" (non-recursive) recipes make will close those file descriptors before spawning the recipe (so that (a) recipes can't accidentally mess up that communication, and (b) because some programs / scripts are written to expect specific file descriptors are unused). For recipes that are determined to be recursive invocations, make will not close those file descriptors.

GNU make uses two different ways to detect whether a recipe is a recursive make, or not. One of them is that you can add the + special character to the beginning of the recipe. The second is the presence of the $(MAKE) (or ${MAKE}) variable references in the recipe.




回答2:


On some systems, the executable calling make is not called make. For example, non-GNU systems often have a make command that is not GNU Make, and users have to run gmake if they want to use Makefiles designed for GNU Make. If you make recursive calls as make on these systems, the recursive calls will not call the same executable as the top-level one typed by the user. Using $(MAKE) ensures recursive calls will use the same "make" as the toplevel.



来源:https://stackoverflow.com/questions/50510278/makefile-why-always-use-make-instead-of-make

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!