Introduction to Sun WorkShop | ![]() ![]() ![]() ![]() ![]() |
The
make
Utility and MakefilesYou can use the
make
utility and makefiles to help automate building of an application with Sun WorkShop. This appendix provides some basic information about themake
utility, makefiles, and makefile macros. It also refers you to dialog boxes in Sun WorkShop that allow you to set makefile options and to add, delete, and override makefile macros. To build your programs without writing your own makefile, see Building a Program and Building With Default Values.The
make
utility applies intelligence to the task of program compilation and linking. Typically, a large application might exist as a set of source files andINCLUDE
files, which require linking with a number of libraries. Modifying any one or more of the source files requires recompilation of that part of the program and relinking. You can automate this process by specifying the interdependencies between files that make up the application along with the commands needed to recompile and relink each piece. With these specifications in a file of directives,make
insures that only the files that need recompiling are recompiled and that relinking uses the options and libraries you want.For more information, there are commercially published books on how to use
make
as a program development tool, including Managing Projects with make, by Oram and Talbott, from O'Reilly & Associates.The Makefile
A file called
makefile
tells themake
utility in a structured manner which source and object files depend on other files. It also defines the commands required to compile and link the files.Each file to build, or step to perform, is called a target. Each entry in a makefile is a rule expressing a target object's dependencies and the commands needed to build or
make
that object. The structure of a rule in the makefile is:
target:
dependencies-listTAB
build-commandsFor the dependencies, each entry starts with a line that names the target file, followed by all the files the target depends on. For the build commands, each entry has one or more subsequent lines that specify the Bourne shell commands that will build the target file for this entry. Each of these command lines must be indented by a tab character.
Fortran 77 Example
You have a program consisting of the following source files and a makefile:
- makefile
commonblock
computepts.f
pattern.f
startupcore.f
Both
pattern.f
andcomputepts.f
have anINCLUDE
ofcommonblock
, and you wish to compile each.f
file and link the three relocatable files, along with a series of libraries, into a program calledpattern
.The makefile contains the following lines.
CODE EXAMPLE B-1 Fortran 77 Makefile
The first line of this makefile indicates that making
pattern
depends onpattern.o
,computepts.o
, andstartupcore.o
. The next line and its continuations give the command for makingpattern
from the relocatable.o
files and libraries.C++ Example
You have a program consisting of the following source files and a makefile:
manythreads.cc
Makefilemany.cc
thr.cc
misc.h
defines.h
many
manythreads
thrI
The makefile contains the following lines.
CODE EXAMPLE B-2 C++ Makefile
The first line of this makefile groups a set of targets with the label
all.
The succeeding lines give the commands for making the three targets, each of which has a dependency on one of the source files.The
make
UtilityTo start the
make
utility, type the following at a command line:
%
make
You can add a number of options to the
make
command for your application using the Build Options dialog box in Sun WorkShop (see Specifying Build Options).The
make
utility looks for a file namedmakefile
orMakefile
in the current directory and takes its instructions from that file.
- Reads
makefile
to determine all the target files it must process, the files they depend on, and the commands needed to build them- Finds the date and time each file was last changed
- Rebuilds any target file that is older than any of the files it depends on, using the commands from
makefile
for that targetTo make writing a makefile easier, the
make
utility has default rules that it uses depending on the suffix of a target file. Recognizing the.f
suffix,make
uses thef77
compiler, passing as arguments any flags specified by theFFLAGS
macro, the-c
flag, and the name of the source file to be compiled.CODE EXAMPLE B-3 demonstrates this rule twice.
CODE EXAMPLE B-3make
Default
make
uses default rules to compilecomputepts.f
andstartupcore.f
. Similarly, the suffix rules for.f95
files invoke thef95
compiler.Macros
The
make
utility's macro facility allows simple parameterless string substitutions. For example, the list of relocatable files that make up the target programpattern
can be expressed as a single macro string, making it easier to change. See also themake
(1S) man page for information aboutmake
macros.A macro string definition has the form:
%
make
NAME
=
stringUse of a macro string is indicated by
$(
NAME)
, which is replaced bymake
with the actual value of the macro string named.This example adds a macro definition naming all the object files to the beginning of
makefile
:
OBJ=pattern.o computepts.o startupcore.oNow the macro can be used in both the list of dependencies as well as on the
f77
link
command for targetpattern
inmakefile
:
pattern: $(OBJ)f77 $(OBJ) -lcore77 -lcore -lsunwindow \
-lpixrect -o patternFor macro strings with single-letter names, the parentheses can be omitted.
You can use the Make Macros dialog box in Sun WorkShop to add macros to or delete macros from the Macros list in your WorkShop target and reassign values for makefile macros in the list. For more information, see Using Makefile Macros.
The initial values of makefile macros can be overridden with command-line options to
make
. For example, you have the following line at the top ofmakefile
:
FFLAGS=-u
You also have the compile-line of
computepts.f
:
f77 $(FFLAGS) -c computepts.f
You have the final link:
f77 $(FFLAGS) $(OBJ) -lcore77 -lcore -lsunwindow \
lpixrect -o pattern
A
make
command without arguments uses the value ofFFLAGS
set above. However, this can be overridden from the command line:
%
make "FFLAGS=-u -O"
The definition of the
FFLAGS
macro on themake
command line overrides themakefile
initialization, and both the-O
flag and the-u
flag are passed tof77
.FFLAGS=
can also be used on the command line to reset the macro so that it has no effect.
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |