views:

70

answers:

4

Why do I need to make a make target before being able to build my source code?

More specifically, what is make target exactly?

+2  A: 

A makefile will usually include code to make your program, install your program, clean up afterward, and other things.

so the word target could be various keywords, such as all, install, clean, etc.

It's a way of saying make something. make all implies do everything

pavium
Strictly, of course, the command '`make all`' only implies (states) "do the work necessary to ensure that 'all' is up to date". Normally, people write the makefile so that does most of what you want (but it doesn't do the install automatically, for example). But that is just a convention.
Jonathan Leffler
A: 

A 'make target' is basically a file that you want rebuilt.

Make can't divine what you want built, so you have to tell it, implicitly or explicitly, what it should build. Often, the first target in the file is a name such as 'all' and if you run 'make' without any explicit target, it will build the first target listed in the makefile. However, some makefiles do not specify any target; then you must specify one on the command line. Or, if you don't want the default target built, then you must specify the target that you do want built.

A target can also be 'phony', in the terms of GNU make. That is, it is a name that does not exist, and the rules do not create it, but it (the phony target) depends on a number of other files that do have rules associated with them. Indeed, the 'all' target is usually a phony target - there isn't a file called 'all' in most directories.

Jonathan Leffler
A: 

make is a common development tool, which checks file dates between source code files and the object code produced from them, and compiles the one where the source is newer. It does this by use a file, called a makefile, which list the files that need to be compared.

The standard syntax is make /f makefile myprog.exe

That says to build myprog.exe using the file list in makefile. The makefile defaults to makefile.mak so if you use that name, you don't have to specify it on the command line.

myprog.exe here is called the target. One trick is that you could put in the makefile a list of build instructions for a non-existant file. If you then say to build that target, it will run those command. Often, you'll see something like make clean, which has temporary file deleted.

James Curran
+3  A: 

Makefiles look like this:

all: mybinary

mybinary: files.o it.o depends.o on.o
[tab]$(CC) $(CFLAGS) files.o it.o depends.o on.o -o mybinary

files.o: files.c files.h
[tab]$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@

...

This means, when you type make all (the shorthand is just to type make), it will make sure that the mybinary target or file is up to date. To do that, it needs to make sure that mybinary is newer than all of files.o it.o depends.o and on.o. If not, then it uses the shell commands specified on the next line to build mybinary. But before doing that, it first makes sure that files.o and so on are up to date. This means they have to be newer than files.c and files.h. If not, then it uses the shell commands specified on the next line to build files.o. Once all the *.o files are up to date, it can build mybinary. Once mybinary is built, the all target is satisfied.

Targets are usually just files. The format of a block is:

target: list of dependency files
[tab]shell commands to build target if it's older than any of its dependencies
[tab]more shell commands
[tab]still more

Targets can also be specified using wildcards, for instance %.c means what in the shell you'd call *.c.

Some targets are "phony" targets meaning they don't correspond to any real file. The "all" target is of this sort. So too is the "clean" target (make clean). And so on. You don't really need or want to build a file called "all" or "clean". There are various ways to specify that a target is phony.

The first target to appear in the Makefile is the one that will be invoked if you simply type make. One convention is to name this target "all". So then make will be the same as make all.

profjim