2

In the case of building multiple targets in makefile, when I say 'make' it by default makes all the target. Is it possible to change the default of all to something else ?

all : target1 target2 

Now , when I give the command 'make' , i only want target1 to build.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
dharag
  • 299
  • 1
  • 8
  • 17

3 Answers3

4

Like Carl mentions, you can just remove target2 from the line.

However if you still want all to make target1 and target2 there are some options...

You did not say which make you were using, which on these fine details it depends on the version, but I'm going to detail how it works for GNU make.

In GNU make, the first goal becomes the default. So if you have a Makefile like:

next:
        @echo "next"

all:
        @echo "All"

It will print:

$ make
next

You can also change the default goal:

.DEFAULT_GOAL=other

next:
        @echo "next"


all:
        @echo "All"

other:
        @echo "other"

It will show:

$ make
other

This is with:

$ make --version
GNU Make 3.81
njsf
  • 2,729
  • 1
  • 21
  • 17
  • So .DEFAULT_GOAL is the standard variable and that target would be build when no other target name is specified. Is that correct ? – dharag Feb 07 '13 at 16:45
  • `DEFAULT_GOAL` is not a standard variable, although it is recognized by gnu make. The standard behavior is for the first rule specified in the Makefile to be the default. – William Pursell Feb 07 '13 at 16:54
  • .DEFAULT_GOAL is documented by the GNU make info page. As I noted in my answer, I assumed GNU make, and may not work on other makes. – njsf Feb 07 '13 at 20:01
  • I used GNU make and it worked very well, thank you ! – dharag Feb 07 '13 at 21:02
0

Sure, just delete target2 from that line.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

Just add the line:

target1:

before the rule defining all. The name all is not special: it becomes the default by virtue of being the first rule listed. By adding target1: before that line, target1 becomes the default. Note that it is not standard behavior to allow multiple definitions of the rule (this is basically a fake rule whose behavior is defined later in the Makefile), but this will work with gnu make. If it doesn't work, just move the actual definition of target1 before the definition of all.

William Pursell
  • 204,365
  • 48
  • 270
  • 300