• Keine Ergebnisse gefunden

Creating a Makefile

A makefile contains one or more lines of text called dependency lines. A dependency line shows how a given file depends on other files and what commands are required to bring a file up to date.

A dependency line has the form:

target : [dependent . . . ] [ ; command

where target is the filename of the file to be updated, dependent is the filename of the file on which the target depends, and

command is the XENIX command needed to create the target file. Each dependency line must have at least one command associated with it, even if it is only the null command (;).

You can give more than one target filename or dependent filename if desired. Each filename must be separated from the next by at least one space. The target filenames must be

separated from the dependent filenames by a colon (:).

Filenames must be spelled as defined by the XENIX system.

Shell metacharacters, such as star (*) and question mark (?), can also be used.

You can give a sequence of commands on the same line as the target and dependent filenames, if you precede each command with a semicolon (;). You can give additional commands on following lines by beginning each line with a tab character.

Commands must be given exactly as they would appear on a shell command line. The at sign (@) can be placed in front of a command to prevent make from displaying the command before executing it. Shell commands, such as cd (C), must appear on single lines; they must not contain the backslash and newline character combination.

You can add a comment to a makefile by starting the comment with a number sign (#) and ending it with a newline character.

All characters after the number sign are ignored. Comments can be placed at the end of a dependency line if desired. If a

command contains a number sign, it must be enclosed in double quotation marks (").

If a dependency line is too long, you can continue it by typing a backslash \ and a newline character.

Keep the make file in the same directory as the given source files.

For convenience, the filenames makefile, Makefile, s.makefile, and s.Makefile are provided as default filenames. These names are used by make if no explicit name is given at invocation. You can use one of these names for your makefile, or choose one of your own. If the filename begins with the s. prefix, make assumes it is an

sces

file and invokes the appropriate SCCS command to retrieve the lastest version of the file.

To illustrate dependency lines, consider the following example. A program named prog is made by linking three object files, x.o, y.o, and z.o . These object files are created by compiling the C

language source files x.c, y.c, and z.c . Furthermore, the files x.c and y.c contain the line:

#include "defs"

This means that prog depends on the three object files, the object files depend on the C source files, and two of the source files depend on the include file defs . You can represent these relationships in a makefile with the following lines.

prog: x.o y.o z.o

ee x.o y.o z.o -0 prog x.o: x.e defs

ee -e x.e y.o: y.e defs

ee -e y.e z.o: z.e

ee -e z.e

In the first dependency line, prog is the target file and x.o, y.o and z.o are its dependents. The command sequence:

ee x.o y.o z.o -0 prog

on the next line tells how to create prog if it is out of date. The program is out of date if anyone of its dependents has been modified since prog was last created.

The second, third, and fourth dependency lines have the same form, with the x.o, y.o, and z.o files as targets and x.c, y.c, Z.c, and defs files as dependents. Each dependency line has one command sequence that defines how to update the given target file.

Invoking make

Once you have a makefile and wish to update and modify one or more target files in the file, you can invoke make by typing its name and optional arguments. The invocation has the form:

make [ option]

macdef+ ]

[

[ target] . . .

where option is a program option used to modify program operation, macdef is a macro definition used to give a macro a value or meaning, and target is the filename of the file to be

updated. It must correspond to one of the target names in the makefile. All arguments are optional. If you give more than one argument, you must separate them with spaces.

You can direct make to update the first target file in the makefile by typing just the program name. In this case, make searches for the files makefile, Makefile, s.makefile, and s.Makefile in the current directory, and uses the first one it finds as the makefile.

For example, assume that the current makefile contains the dependency lines given in the last section. Then the command:

make

compares the current date of the prog program with the current date each of the object files x.o, y.o, and z.o . It recreates prog if any changes have been made to any object file since prog was last created. It also compares the current dates of the object files with the dates of the four source files x.c, y.c, z.c, or defs and recreates the object files if the source files have changed. It does this before recreating prog so that the recreated object files can be used to recreate prog . If none of the source or object files have been altered since the last time prog was created, make announces this fact and stops. No files are changed.

You can direct make to update a given target file by giving the filename of the target. For example,

make x.o

causes make to recompile the x.o file, if the x.c or defs files have changed since the object file was last created. Similarly, the command

make x.o z.o

causes make to recompile X.D and z.o if the corresponding dependents have been modified. The make program processes target names from the command line in a left to right order.

You can specify the name of the make file you wish make to use by giving the -f option in the invocation. The option has the form:

-f fil ename

where filename is the name of the makefile. You must supply a full pathname if the file is not in the current directory. For example, the command:

make -f makeprog

reads the dependency lines of the make file named makeprog found in the current directory. You can direct make to read dependency lines from the standard input by giving "-" as the filename. The make program reads the standard input until the end-of-file character is encountered.

You can use the program options to modify the operation of the make program. The following list describes some of the options.

-p Prints the complete set of macro definitions and dependency lines in a makefile.

-i Ignores errors returned by XENIX commands.

-k Abandons work on the current entry, but continues on other branches that do not depend on that entry.

-s Executes commands without displaying them.

-r Ignores the built-in rules.

-0 Displays commands but does not execute them. The make program even displays lines beginning with the at sign (@).

-e Ignores any macro definitions that attempt to assign new values to the shell's environment variables.

-t Changes the modification date of each target file without recreating the files.

Note that make executes each command in the make file by

passing it to a separate invocation of a shell. Because of this, care must be taken with certain commands (for example, cd and shell control commands) that have meaning only within a single shell process; the results are forgotten before the next line is executed.

If an error occurs, make normally stops the command.

ÄHNLICHE DOKUMENTE