How does “make” app know default target to build i

2019-01-08 05:08发布

Most linux apps are compiled with:

make
make install clean

As I understood it, make takes names of build targets as arguments. So install is a target that copies some files and after that clean is a target that removes temporary files.

But what target will make build if no arguments are specified (e.g. first command in my example)?

标签: makefile
3条回答
再贱就再见
2楼-- · 2019-01-08 05:59

By default, it begins by processing the first target that does not begin with a '.' aka the default goal; to do that, it may have to process other targets - specifically, ones the first target depends on. The GNU make manual covers all this stuff, and is a surprisingly easy and informative read.

查看更多
The star\"
3楼-- · 2019-01-08 05:59

GNU Make also allows you to specify the default make target using a special variable called .DEFAULT_GOAL. You can even unset this variable in the middle of the makefile, causing the next target in the file to become the default target.

Really interesting but esoteric stuff. But I agree with anon up there - the gnu make manual is quite readable. You might think that you don't need to know most of the stuff in it, but once you see an really comprehensive one, you would really appreciate some really fancy stuff that you can do with it - string manipulation, file/directory functions, etc.

Ref: The Gnu Make manual - Special Variables

查看更多
贪生不怕死
4楼-- · 2019-01-08 06:03

To save others a few seconds, and to save them from having to read the manual, here's the short answer. Add this to the top of your make file:

.DEFAULT_GOAL := mytarget

mytarget will now be the target that is run if "make" is executed and no target is specified.

If you have an older version of make (<= 3.80), this won't work. If this is the case, then you can do what anon mentions, simply add this to the top of your make file:

.PHONY: default
default: mytarget ;

References: https://www.gnu.org/software/make/manual/html_node/How-Make-Works.html

查看更多
登录 后发表回答