A bit more
The material presented so far only scratches the surface
of the features of the make utility.
make has many default and implicit rules
for how to build objects from source code. These additional
features and rules make make very powerful.
However, the rules are often not obvious and can vary from
platform to platform, which can make make
frustrating and difficult to use. Books devoted to make
are available for those who are interested in learning all the
details.
In this section, we'll discuss a few additional features of make.
All we will do is slightly rework the previous makefile.
Here's a makefile that maintains the same program we've been discussing.
The modifications are described below.
FC = xlf90
FCOPTS = -O3
LD = xlff90
LDOPTS =
EXENAME = project
OBJS = project.o print_project_info.o
$(EXENAME) : $(OBJS)
$(LD) $(LDOPTS) -o $(EXENAME) $(OBJS)
print_project_info.o : print_project_info.f project_info.o
$(FC) $(FCOPTS) -c project_info.o print_project_info.f
.f.o :
$(FC) $(FCOPTS) -c $<
clean:
rm -f core *.o
clobber: clean
rm -f $(EXENAME)
Macros
You can define macros, or symbols that stand for other
things, in makefiles. The lines at the beginning of the
makefile are macros. For example
FC = xlf90 is a macro. Everywhere that
$(FC) appears in the makefile,
it will be replaced with xlf90.
These macro definitions are an easy way to change items that
appear in many places in the makefile. Notice that we defined
the macro FCOPTS = -O3 and placed it in all the
xlf90 command options. The -O3 option to
xlf90 specifies an optimization level. If we to change the
optimization level for the entire code,
we just change the one line in which
FCOPTS is defined.
Implicit suffix rules
You can give make a set of rules for creating
files with a certain suffix from files with the same root
file name, but a different suffix. For example, the line
tells make that all .o files
are created from the corresponding .f files.
The command in the next line will recompile any .f
file if it is newer than the corresponding .o file.
This line uses the make internal macro $<,
which translates to "any dependency that is more recent than
its corresponding target." This internal macro can only be
used in suffix rules.
Exceptions to the suffix rule can be stated explicitly, as
is done here for the print_project_info.o object,
which needs the project_info.o module information.
Additional targets
We've added two useful targets in this makefile:
clean and clobber. If you
type make at the command line, make
makes the first target it encounters in the makefile,
in this case the executable program. If you type make
with an argument, make jumps to the target with the
name of the command line argument.
For example, make clean jumps to the clean
target. The two new targets perform these tasks
clean
- Since the file "clean" does not exist,
make clean
will always execute the command rm -f *.o, which
deletes the .o files. This is useful if you want
to get rid of all those files after you're finished building your
program.
clobber
-
make clobber will get rid of the executable file
as well as the .o files. The file clobber also
does not exist so make clobber will always cause
the executable to be removed, and since it depends on clean,
it will also imply make clean.
|