The makefile

xiaoxiao2021-03-06  60

The makefile

Comments Rules Dependency Lines Shell Lines Macros Macro Modifiers Inference Rules Response Files Makefile Directives

Make reads its instructions from text files. An initialization file is read first, followed by the makefile. The initialization file holds instructions for all "makes" and is used to customize the operation of Make. Make automatically reads the initialization file whenever it starts up THPICALLY THE INITION FILE IN THE DIRECTORY OF IN PAGE. The name and location of the initialization file is discussed in detail on page.

.................. ..

With a few exceptions, the initialization file holds the same kind of information as does a makefile Both the initialization file and the makefile are composed of the following components:. Comments, dependency lines, directives, macros, response files, rules and shell lines.

Continued Makefile Lines

LING. FOR Easier Reading a long line can be broken up by putting "/ enter" as the last characters of this line and the rest of this (Logical) line on the next (physical) line of the next Makefile. for example:

First_PART_OF_LINE SECOND_PART_OF_LINE

Is The Same As:

First_PART_OF_LINE / Second_Part_OF_LINE

Comments [TOP]

The simplest makefile statement is a comment, which is indicated by the comment character "#" All text from the comment character to the end of the line is ignored Here is a large comment as might appear in a makefile to describe its contents..:

#

# Makefile for Opus make 6.1

#

# Compiler: Microsoft C 6.0 # Linker: Microsoft Link 5.10

#

The Comment Character Could Also Be Used At The End of another makefile Statement:

Some Makefile Statement # a comment

Comments and continued makefile Lines

IF "/ enter" APPEARS ON A Commented Line, The Comment Acts Until The End of The Line and The Following Line Is Still Continued. For Example:

LINE_ONE /

LINE_TWO # more_line_two /

LINE_THREE

Is The Same As:

LINE_ONE LINE_TWO LINE_THREE

Rules [TOP]

A rule tells Make both when and how to make a file. As an example, suppose your project involves compiling source files main.c and io.c then linking them to produce the executable project.exe. Withholding a detailed explanation for a bit, Here is a makefile Using Borland C Which Will Manage The Task Of Making Project.exe:

The Example Makefile

Project.exe: main.obj o.obj

TLINK C0S main.obj obj, project.exe,, cs / lf: / bc / lib

Main.obj: main.c

BCC-MS -C main.c

IO.OBJ: IO.C

BCC-MS -C IO.c

This makefile shows three rules, one each for making project.exe, main.obj, and io.obj. The rules as shown above are called explicit rules since they are supplied explicitly in the makefile. Make also has inference rules that generalize the make Inference. Inference Rules Are Discussed on Page and in The User's Guide on page.

We will return to and modify this example during the course of this tutorial.

Dependency lines: when to build a target [TOP]

THE LINES with the colon ":" in Them Are Called Dependency Lines. They Determine When the Target is to Be Rebuilt.

To the left of the colon is the target of the dependency To the right of the colon are the sources [1] needed to make the target A dependency line says "the target depends on the sources." For example, the line..: Project.exe: main.obj o.obj

states that project.exe depends on main.obj and io.obj. At run time Make compares the time that project.exe was last changed to the times main.obj and io.obj were last changed. If either source is newer than project . The last-change time is The target's Time AS It Appers in The File-System Directory. This Time Is Also Known As The Target's TimeStamp.

The make process is recursive

IT is a Basic Feature of Make That A Target's Sources Are Made Before The TimeStamp Comparison Occurs. The line:

Project.exe: main.obj o.obj

Implies "make main.obj and obj before committing their timestamps with project.exe." in turn:

Main.obj: main.c

Says "make main.c before.obj." You can see what if main.c is newer tour.obj, main.obj Will be rebuilt. Now main.obj Will Be.com, Causeing project.exe to be rebuilt.

Additional Dependencies

In C and in other programming languages ​​it is possible to include the contents of a file into the file currently being compiled. Since the compiled object depends on the contents of the included file, we add the included file as a source of the object file.

Assume Each of main.c and o.c include deflude defl.h. we can Either change Two dendency Lines in the makefile:

Main.obj: main.c becomes main.obj: main.c def.h

IO.OBJ: IO.C BECOMES IO.OBJ: IO.C Def.h

OR Add A New Line Which Lists Only The Additional Dependencies:

Main.obj o.obj: def.h

Notice that there are two targets on the left of the colon This line means that both main.obj and io.obj depend on def.h. Either of these methods are equivalent The example makefile now looks like:.. Project.exe: main .Obj o obj

TLINK C0S main.obj obj, project.exe,, cs / lf: / bc / lib

Main.obj: main.c

BCC-MS -C main.c

IO.OBJ: IO.C

BCC-MS -C IO.c

Main.obj o.obj: incl.h

Shell Lines: how to build a target [top]

The indeted lines That Follow Each Dependency Line Are Called Shell Lines. Shell Lines Tell Make How To Build The Target. For Example:

Project.exe: main.obj o.obj

TLINK C0S main.obj obj, project.exe,, cs / lf: / bc / lib

.

For TLINK, C0S Is The SMALL Model Start-Up Object File and the cs is The small model library. The / lf: / bc / lib flag tells Tlink That The Start-Up Object File and Library Files Can Be found in the f: / bc / lib Directory.

A Target Can Have More One Shell Line, Listed One After The Other, Such as:

Project.exe: main.obj o.obj

Echo Linking Project.exe

TLINK C0S main.obj obj, project.exe,, cs / lf: / bc / lib> TLINK.out

..........................

After each shell line is executed, Make checks the shell line exit status. By convention, programs return a 0 (zero) exit status if they finish without error and non-zero if there was an error. The first shell line returning a non- Zero EXIT STATUS Causes Make to Display To Message:

OPUS MAKE: Shell line exit status exit_status Stop.This usually means the program being executed failed Some programs return a non-zero exit status inappropriately and you can have Make ignore the exit status by using a shell-line prefix Prefixes are characters... That Appear Before The Program Name and Modify The Way Make Handles The Shell Line. for Example:

Project.exe: main.obj o.obj

- TLINK C0S main.obj obj, project.exe,, cs / lf: / bc / lib

The "-" Prefix Tells make to ignore the exit status of shell line. If the exit status was non-zero make 10 Display the message:

Opus make: shell line exit status exit_status (ignored)

See Page for more information on shell Lines and shell-line prefixes.

Macros [TOP]

The Example Makefile is reproducesd here:

Project.exe: main.obj o.obj

TLINK C0S main.obj obj, project.exe,, cs / lf: / bc / lib

Main.obj: main.c

BCC-MS -C main.c

IO.OBJ: IO.C

BCC-MS -C IO.c

Main.obj o.obj: def.h

We See That TEXT "main.obj obj" Occurs REPEATEDLY. To Cut Down The Amount of Repeated Text, We can use a macro definition to assign a symbol to the text.

Defining Macros in the makefile

A Macro Definition Line Is A Makefile Line with a Macro Name, An Equals Sign "=" And A Macro Value. In The Makefile, Expressions of The Form $ (Name) OR $ {name} area replaced with value. If the macro Name Is A Single Letter, The Parentheses or Braces Are Optional (IE $ x, $ (x) and $ {x} all mean "the value of macro x").

Here Is The Above Example Written with The Introduction of Four Macros:

Objs = main.obj ooo.obj

Model = s

CC = BCC

Cflags = -m $ (model)

Project.exe: $ (OBJS)

TLINK C0 $ (MODEL) $ (OBJS), Project.exe,, C $ (Model) / LF: / BC / LIB

Main.obj: main.c

$ (Cc) $ (cflags) -c main.cio.obj: IO.c

$ (Cc) $ (cflags) -c io.c

$ (OBJS): Incl.H

The value of the OBJS macro is the list of object files to be compiled. The macro definitions for MODEL, CC and CFLAGS were introduced so that it is easier to change the compiler memory model, name of the C compiler and its options.

Make Automatically Imports Environment Variables As Macros, SO You CAN Reference An Environment Variable Such As Path with The makefile expression $ (path).

Defining Macros on The Command Line

Macros Can Be Defined on The make command line. For example:

Make cflags = -ms

.

IF a commnd-line macro contains spaces, IT Must Be Enclosed in Double Quotes As:

Make "cflags = -ms-z -P"

Run-Time Macros

Make defines some special macros whose values ​​are set dynamically. These macros return information about the current target being built. As examples, the .TARGET macro is name of the current target, the .SOURCE [2] macro is the name of the inferred source (from an inference rule) or the first of the expendio and the .sources [3] macro is the list of all sources.

Using Run-Time Macros The Example Can Be Written:

Objs = main.obj ooo.obj

CC = BCC

Model = s

Cflags = -m $ (model)

Project.exe: $ (OBJS)

TLINK C0 $ (MODEL) $ (OBJS), $ (. Target), C $ (model) / LF: / BC / LIB

Main.obj: main.c

$ (Cc) $ (cflags) -c $ (. SOURCE)

IO.OBJ: IO.C

$ (Cc) $ (cflags) -c $ (. SOURCE)

$ (OBJS): Incl.H

As you can see, the shell lines for updating main.obj and io.obj are identical when run-time macros are used. Run-time macros are important for generalizing the build process with inference rules, as shown on Page .Macro Modifiers [ TOP]

Macros are used to reduce the amount of repeated text. They are also used in inference rules to generalize the build process. We often want to start with the value of a macro and modify it in some manner. For example, to get the list of Source Files from The Objs Macro We can do:

SRCS = $ (Objs, .obj = .c)

This Example Uses the "from = to" macro modifier to replace the from text in the expansion of objs with the to text. The result is what $ (srcs) IS "main.c io.c". In general, to modify a Macro Expand It with:

$ (Name, Modifier [, Modifier ...])

.

FileName Components

There is a set of macro modifiers for accessing parts of file names. For example, with the macro definition:

SRCS = D: /SRC /MAIN.C IO.ASM

Some of the model:

Modifier, And Description Example Valued, The Directory $ (SRCS, D) D: / SRC .E, THE EXTENSION (SRCS, E) .c .asmf, The File Name $ (srcs, f) main. C o.Aasm

Tokenize

Another modifier is the "Wstr" modifier, which replaces whitespace between elements of the macro with str, a string. The str can be a mix of regular characters and special sequences, the most important sequence being "/ n" which represents a newline character (Like Hitting the Enter Key). for esample:

$ (Objs, W Space / N) is main.obj

IO.OBJ

Other Modifiers

Other Modifiers include: "@" (LOWER CASE), "UC" (UC "," MEMBER) AND "N". The "M" and "N" modifiers and the "s" modifier use regular expression for powerful and flexible pattern-matching. See Page for more information on all macro modifiers.inference rules [TOP]

Inference rules generalize the build process so you do not have to give an explicit rule for each target. As an example, compiling C source (.c files) into object files (.obj files) is a common occurrence. Rather than requiring a Statement That Each .Obj File Depends ON A Like-named .c file, make uses an inference rule to infer what defendency. The source determined by an inference rule.

Inference rules are rules distinguished by the use of the character "%" in the dependency line. The "%" (rule character) is a wild card, matching zero or more characters. As an example, here is an inference rule for building. Obj Files from .c files:

% .Obj:% .c

$ (Cc) $ (cflags) -c $ (. SOURCE)

This Rule States That a .obj File Can Be Built from a Corresponding .c file with the shell line "$ (cc) $ (cflags) -c $ (. Source)". The .c and .obj files share the same root Of the file name.

When the Source and Target Have The Same File Name Except for Their Extensions, This Rule Can Be Specified in An Alternative Way:

.c.obj:

$ (Cc) $ (cflags) -c $ (. SOURCE)

The Alternative Form Is Compatible with opus make prior to this version and withless. .. ..

Make Predefines The "% .Obj:% .c" Inference Rule As Listed Above So The Example We Have Been Working ON NOW BECOMES MUCH SIMPLER:

Objs = main.obj ooo.obj

CC = BCC

Model = s

Cflags = -m $ (model)

Project.exe: $ (OBJS)

TLINK C0 $ (MODEL) $ (OBJS), $ (. Target), C $ (model) / LF: / BC / LIB

$ (OBJS): Incl.H

Response files [TOP]

For MS-DOS, OS / 2 & WIN95 THE IS A Rather Severe Restriction On The Result That The Shell Line With Too Short For Many Compilers and Far Too Short for Linkers and librarians.

. To overcome this restriction many programs can receive command-line input from a response file Opus Make has two kinds of support for response files: automatic response files, where Make decides when to build a response file or; inline response files, where you write Response File-Creating Statements Directly in the makefile.

Automatic Response Files

Make has predefined support for several linkers, librarians and compilers, and you can augment Make's support by writing your own definitions (see Page) With Make's predefined support you can just add the following statement to your makefile.:

.Response.link: TLINK

This tells Make that the program tlink accepts LINK-style response files. When a shell line executes tlink, Make checks if the shell line is longer than allowed by the operating system and automatically produces a response file if necessary.

Inline Response Files

Response Files Can Also Be Coded "Inline" in Your makefile. Here is The Tlink Shell Line of the Example, Written To Use An Inline Response File:

Project.exe: $ (OBJS)

TLINK @ <<

C0 $ (Model) $ (. Sources, W / N)

$ (. Target)

C $ (model) / LF: / BC / LIB

<<

The tlink program is invoked as "tlink @response_file" where response_file is a name generated by Make The "W / n" macro modification replaces whitespace between elements of $ with " enter" The response_file contains. (SOURCES.):. C0s main .Obj

IO.OBJ

PROJECT.EXE

C0 / f: / bc / lib

Makefile Directives [TOP]

. Makefile directives control the makefile lines Make reads at read time Here is our example extended with conditional directives (% if,% elif,% else and% endif) to support both Borland and Microsoft compilers Comments have been added for documentation.:

# This makefile compiles the project listed in The Proj Macro

#

Proj = Project # the name of the project

Objs = main.obj o.obj # list of object files

# Configuration:

#

Model = s # memory model

CC = BCC # Name of Compiler

# Compiler-Dependent Section

#

% IF $ (cc) == BCC # ff Compiler IS BCC

CFLAGS = -M $ (MODEL) # $ (cflags) IS -MS

LDStart = c0 $ (model) # The start-up Object File

LDLIBS = C $ (model) # The library

LDFLAGS = / LF: / BC / LIB # f: / BC / LIB IS LIBRARY DIRECTORY

% Elif $ (CC) == Cl # else if Compiler Is Cl

CFLAGS = -A $ (Model, UC) # $ (cflags) is -as

LDStart = # no special start-up

LDLIBS = # No Special Library

LDFLAGS = / LF: / C6 / lib; # f: / c6 / lib is library directory

% Else # else

% Abort Unsupported CC == $ (CC) # Compiler Is Not Supported

% Endif # ENDIF

# The project to be built

#

$ (Proj) .exe: $ (OBJS)

TLINK $ (LDSTART) $ (OBJS), $ (. Target), $ (LDLIBS) $ (ldflags)

$ (OBJS): Incl.H

THE LAYOUT OF This Makefile Is Fairly Traditional - Macros Are Defined First, The Primary Target Follows The Macros and The Extra Dependency Information IS Last.

This example also uses the% abort directive to abort Make if the makefile does not support a particular compiler. Directives can also be used at run time, to control the shell lines Make executes. For more information about directives, see Page .Footnotes

[1] You May Be Accustomed to "Source" Meaning A File Containing Source Code. We will say "Source File" When We Mean this.

[2] this was caled. Implicit in Opus make v5.2x and you can set. ..

[3] This Was Called. Allow in Opus make v5.2x and you can set. ..

转载请注明原文地址:https://www.9cbs.com/read-118431.html

New Post(0)