272

Most Linux apps are compiled with:

make
make install clean

As I understand it, the make command takes names of build targets as arguments. So for example install is usually a target that copies some files to standard locations, and clean is a target that removes temporary files.

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

waldyrious
  • 3,683
  • 4
  • 33
  • 41
grigoryvp
  • 40,413
  • 64
  • 174
  • 277

4 Answers4

312

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.

Marcel Gosselin
  • 4,610
  • 2
  • 31
  • 54
  • Not the target "all" as previous answer states? :( – grigoryvp Jan 13 '10 at 15:21
  • 98
    Calling the first target `all` is just a convention. – Tobu Jan 13 '10 at 15:23
  • And if i issue "make install" it will only build "install" target. skipping first one is "install" don't depend on it, correct? – grigoryvp Jan 13 '10 at 15:27
  • Correct, but 'install' almost certainly DOES depend on it - 'install' is typically the most dependent rule, because it needs all the exes, libraries etc. Of course, you can also write an 'install' rule that just does a copy. –  Jan 13 '10 at 15:29
  • Historically, the advice has always been to do 'make && make install' rather than relying on the dependencies of the install target. Many projects will not build correctly (or at all) if you try to just do 'make install'. – William Pursell Jan 13 '10 at 16:00
  • 1
    It's useful to run them separately if you're installing for other users, as you then only need to run the `make install` in sudo, rather than the entire build. – Scott Wales Jan 14 '10 at 06:19
  • 4
    FYI the default man entry for make makes no mention of what happens when you don't specify a target.. – Sekm Mar 08 '13 at 00:59
  • 3
    If the GNU make manual covers this stuff, why was this question asked? The manual is great at documenting how to write rules, but makes no mention on how to write a Makefile. – Nick Dec 31 '14 at 16:35
  • @anon What if the makefile include a .mk file before its first target? will it execute the first target of .mk file or its own first target? – demonguy Jan 28 '15 at 03:03
  • 1
    I'm looking at a make file that `include`s another makefile, whose first target is `%.o : %.cpp`. So the default target is... to build all .cpp files? – Qwertie Nov 09 '18 at 19:55
295

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

Alison R.
  • 4,204
  • 28
  • 33
Samuel
  • 8,063
  • 8
  • 45
  • 41
  • 2
    @nathan It makes mytarget the default target if someone runs "make" without any parameters. – Samuel Jan 15 '16 at 14:55
  • 1
    Note that .DEFAULT_GOAL doesn't appear to be supported in GNU make v3.80, and I would assume prior versions as well (v3.81 does support it though). If you are running an older version of make you will have to make sure your default target is the first/topmost target in your make file. – Samuel Mar 25 '16 at 19:15
  • 1
    is `default:` a special target name, or this works because it's the first target in the file? – Anentropic Sep 23 '16 at 09:19
  • I know Samuel says it but it does indeed appear that .PHONY and default: mytarget should be before mytarget at the least (or at top of file as mentioned) – Colin D Mar 15 '17 at 02:05
  • 14
    "...to save them from having to read the manual" is one of the points of SO: quick answers to questions that would otherwise take awhile to dig for in ancient documentation. – WattsInABox Dec 14 '17 at 16:22
  • 1
    "Add this to the top of your make file": Adding to the top does not seem to be a requirement. – Aman Deep Gautam Mar 09 '18 at 01:52
  • 4
    This should be the correct answer. Explicit over implicit wins in this case. – Sion Sep 14 '18 at 09:05
46

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.

Ref: The Gnu Make manual - Special Variables

jottr
  • 3,256
  • 3
  • 29
  • 35
Sandip Bhattacharya
  • 1,042
  • 10
  • 11
1

bmake's equivalent of GNU Make's .DEFAULT_GOAL is .MAIN:

$ cat Makefile
.MAIN: foo

all:
    @echo all

foo:
    @echo foo
$ bmake
foo

See the bmake(1) manual page.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79