0

Where do we put the all in a Makefiles?

I had a similar question found earlier, but I needed a little bit more details. I also looked at the GNU make manual, but got lost in the mountain of documentation. I tried the googles, but didn't find a good example. So, I did what was my last resort and tried to figure it out by hacking at a Makefile myself. I have:

CC=gcc
CFLAGS=-Wall -g

clean: 
rm -f all: ex1

This didn't compile my ex1 in C. Also, if I wanted to add more to this make file. Like ex1, ex2, to exercise whatever. Would I just put the all at the top and repeat the

rm - f whatever

line below clean?

Appreciate your help and assistance. Patience appreciated too.

Community
  • 1
  • 1
GeekyOmega
  • 1,235
  • 6
  • 16
  • 34

1 Answers1

3

all is actually nothing special, just a target name commonly used. To make it work best, it should be the first target in the file. This makes it the default target, and gmake works the same as gmake all.

Normally, the all target has dependencies, which are the things which need to be built. E.g.

all: myexe

rm is related to the clean target (which is also nothing special, just a name commonly used). You define it this way:

clean:
    rm -f myexe

This makes it delete myexe when gmake clean is run. If you makefile builds other files, they should also be listed there.

There's a lot more to know about makefiles. Normally you would use variables and template rules, which help you avoid repeating yourself. But this is far more than a simple answer can describe.

ugoren
  • 16,023
  • 3
  • 35
  • 65
  • When you talk about adding more files. I tried All: myexe1 myexe2 and All: myexe1, myexe2. What am I missing here? I almost got this and once I get this answered can consider this whole question as answered. Thanks! – GeekyOmega Jun 27 '12 at 19:20
  • Defining `all: myexe1 myexe2` is good, but only tells `make` that it should build these two executables. You also need to provide rules to build `myexe1` and `myexe2`, but this is beyond this question. Maheybe [this link](http://mcs.mines.edu/Courses/csci262/INFO/Makefile_Quick_Reference.html) can help. – ugoren Jun 28 '12 at 04:43