Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile: all vs default targets

Talking with respect to GNU make, what is the difference between PHONY targets all: and default:.

CC=g++
default: hello

hello: hello.cpp
     $(CC) -o hello hello.cpp

and

CC=g++
all: hello

hello: hello.cpp
     $(CC) -o hello hello.cpp

Both of them do the same job.

like image 228
Piyush Deshmukh Avatar asked Dec 02 '14 06:12

Piyush Deshmukh


People also ask

What is the default target in makefile?

By default, the goal is the first target in the makefile (not counting targets that start with a period). Therefore, makefiles are usually written so that the first target is for compiling the entire program or programs they describe.

What is all Target in makefile?

all target is usually the first in the makefile, since if you just write make in command line, without specifying the target, it will build the first target. And you expect it to be all . all is usually also a . PHONY target.

What is the difference between make and make all?

'make all' simply tells the make tool to build the target 'all' in the makefile (usually called ' Makefile '). You may have a look at such file in order to understand how the source code will be processed. As about the error you are getting, it looks the compile_mg1g1. ksh shell script is missing.

What is the significance of the order of targets in the makefile?

The order of rules is not significant, except for determining the default goal : the target for make to consider, if you do not otherwise specify one. The default goal is the target of the first rule in the first makefile. If the first rule has multiple targets, only the first target is taken as the default.


3 Answers

You can call them shirley if you like; neither of the labels you mention has any special semantics. The default behavior of make is to run the first target in the Makefile if you don't specify a target as a command-line argument. If you like to override this behavior, there is the .DEFAULT: special target.

There is a convention to have a target named all which builds everything, but this is just human convention, not a special case or a requirement as far as Make is concerned.

Similarly, there is a (weak) convention to call the default target default, but similarly, this is just a human label (and somewhat overlaps and possibly conflicts with the all convention).

So the following Makefile does exactly the same thing:

.PHONY: shirley all default
default: hello
all: hello
shirley: hello

hello: hello.cpp
# (Make already knows how to build an executable out of a .cpp file)

You can omit any or all of the phony targets above, and the only difference will be that humans won't be able to say make shirley when they (effectively) mean make hello.

Bottom line: Construct your Makefile so that make does what a reasonable end-user expects without reading too much README files or inspecting the Makefile. Often that will be make all (and you should probably have a target with this name, just to satisfy human conventions, even if it's not the default) but this obviously depends on your project and your users' expectations.

Don't call me Shirley.

like image 96
tripleee Avatar answered Oct 31 '22 02:10

tripleee


The only difference, is that all is recommended in the GNU Make manual, to be the first (default) target, and default has no such special recommendation.

like image 24
Mark Galeck Avatar answered Oct 31 '22 02:10

Mark Galeck


One is spelled "all" and the other is spelled "default". There's no other difference between them. Maybe if you explained why you're asking we'd be able to help more.

Note that in your example above neither of the targets are actually phony. You'd have to declare them as such:

.PHONY: all

etc.

like image 41
MadScientist Avatar answered Oct 31 '22 02:10

MadScientist