Datasheet
Book VIII
Chapter 1
Programming
in Linux
541
Exploring the Software-Development Tools in Linux
#########################################################
# Sample makefile
# Comments start with ‘#’
#
#########################################################
# Use standard variables to define compile and link flags
CFLAGS= -g -O2
# Define the target “all”
all: xdraw
OBJS=xdraw.o xviewobj.o shapes.o
xdraw: $(OBJS)
# Object files
xdraw.o: Makefile xdraw.c xdraw.h
xviewobj.o: Makefile xviewobj.c xdraw.h
shapes.o: Makefile shapes.c shapes.h
This makefile relies on GNU make’s implicit rules. The conversion of .c
files to .o files uses the built-in rule. Defining the variable CFLAGS passes
the flags to the C compiler.
The target named all is defined as the first target for a reason — if you run
GNU make without specifying any targets in the command line (see the make
syntax described in the following section), the command builds the first
target it finds in the makefile. By defining the first target all as xdraw,
you can ensure that make builds this executable file, even if you don’t explic-
itly specify it as a target. UNIX programmers traditionally use all as the
name of the first target, but the target’s name is immaterial; what matters is
that it’s the first target in the makefile.
How to run make
Typically, you run make by simply typing the following command at the shell
prompt:
make
When run this way, GNU make looks for a file named GNUmakefile,
makefile, or Makefile — in that order. If make finds one of these
makefiles, it builds the first target specified in that makefile. However,
if make doesn’t find an appropriate makefile, it displays the following
error message and exits:
make: *** No targets specified and no makefile found. Stop.
If your makefile happens to have a different name from the default names,
you have to use the -f option to specify the makefile. The syntax of the
make command with this option is
make -f filename
where filename is the name of the makefile.
42_770191-bk08ch01.indd 54142_770191-bk08ch01.indd 541 8/6/10 9:51 AM8/6/10 9:51 AM