How to write a makefile

xiaoxiao2021-03-06  58

How to write a makefile

IntroductionMake is one of the original Unix tools for Software Engineering. By S.I. Feldman of AT & T Bell Labs circa 1975. But there are public domain versions (eg. GNU) and versions for other systems (eg. Vax / VMS).

Related Tools Are The Language Compilers (CC, F77, LEX, YACC, ETC.) and shell Programming Tools (Eg. Awk, sed, cp, rm, etc.). You need to know how to use.

Important Adjuncts Are Lint (Source Coding for Obvious Errors) CTags (Locate Functions, etc. In Source Code) and mkdepend. These is Nice, And Good Programmers Use.

Important, And Related Tools, Are The Software Revision Systems Sccs (Source Cogn Control System) And RCS (Revision Control System - The Recommended Choice)

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

Makefile naming

Make Is Going to Look for a File Called

Makefile, IF not Found Then A File Called

Use The First (So The Name Stands Out in LISTINGS).

You Can Get Away WITHOUT ANY MAKEFILE (But Shouldn't)! Make Has Default Rules It Knows About.

Makefile Components

Comments Comments Are Any Text Beginning with The Pound Anywhere ON A LINE AND CONTITIL THE End of The Line. For example: # $ ID: SLIDES, V 1.2 1992/02/14 21:00 : 58 Reggers EXP $

Macro Definition and Substitution Mechanism. Macros Are Defined In A Makefile AS

=

PAIRS. for esample:

Macros = -me

Psroff = groff -tps

Ditroff = groff -tdvi

Cflags = -o -systype BSD43

. There are lots of default macros - you should honor the existing naming conventions To find out what rules / macros make is using type:% make -pNOTE: That your environment variables are exported into the make as macros They will override the defaults. . You can set macros on the make command line:% make "cflags = -o" "ldflags = -s" Printenv

cc -oprintenv.c -s -o protetenv

Targets you make a particular target (eg. Make all), in none specified tell: Paper.dvi: $ (srcs)

$ (DITROFF) $ (SRCS)> Paper.dvi

NOTE: The the line beginning with $ (DITROFF) begins with TAB not spaces.The target is made if any of the dependent files have changed The dependent files in this case are represented by the $ (SRCS) statement Continuation of Lines Use.. a back slash (/). This is important for long macros and / or rules. Conventional Macros There are lots of default macros (type "make -p" to print out the defaults). Most are pretty obvious from the rules in which they Are used: ar = ar

Gflags =

Get = Get

Asflags =

MAS = MAS

As = as

FC = F77

Cflags =

CC = CC

LDFlags =

LD = LD

LFLAGS =

Lex = lex

YFLAGS =

Yacc = yacc

LoadLibs =

Make = make

Makeargs = 'shell = / bin / sh'

Shell = / bin / sh

Makeflags = B

Special Macros Before Issuing Any Command IN A Target Rule Set There Certain Special Macros PREDEfined.

$ @ is The name of the file to be my name. $? is The names of the change dependents. so, for example, we could Use a rule printenv: printenv.c

$ (CC) $ (cflags) $ (ldflags) -o $ @

Alternatively: Printenv: Printenv.c

$ (Cc) $ (cflags) $ @. C $ (ldflags) -o $ @

There are two more special macros used in implicit rules They are:.. $

[

Command ...]

Items in brackets are optional, ellipsis means one or more. Note the tab to preface each command is required. The semantics is pretty simple. When you say "make target" make finds the target rule that applies and, if any of the dependents are newer than the target, make executes the com- mands one at a time (after macro substitution). If any dependents have to be made, that happens first (so you have a recursion). A make will terminate if any command returns a failure STA-Tus. That's why you see rules like: clean:

-rm * .o * ~ Core Paper

Make ignores the returned status on command lines that begin with a dash. Eg. Who cares if there is no core file? Make will echo the commands, after macro substition to show you what's happening as it happens. Sometimes you might want to turn that OFF. for example: install:

@echo you must be root to install

Example Target Rules for Example, To Manage Sources Stored Withnin RCS (Sometimes You'll Need To "Check Out" a Source File): SRCS = X.C Y.c Z.c

$ (Srcs):

Co $ @

To Manage Sources Stored Withnin SCCS (Sometimes You'll Need To "Get" a Source File: $ (srcs):

SCCS GET $ @

Alternativley, To Manage Sources Stored With SCCS or RCS Let's Generalize with a Macro That We can set as required. Srcs = x.c y.c z.c

# Get = sccs get

Get = co

$ (Srcs):

$ (GET) $ @

For Example, To Construct A Library of Object Files Lib.a: x.o y.o z.oar rvu lib.a x.o y.o z.o

Ranlib Lib.A

Alternatively, to be be a bit more fan you could use: obj = x.o y.o z.o

Ar = ar

LIB.A: $ (OBJ)

$ (Ar) rvu $ @ $ (bj)

RANLIB $ @ @

Since ar is a default macro already assigned to "ar" you can get away without defining it (but shouldn't). If you get used to using macros You'll Be Able To Make A Few Rules That You Can CAN Use over and over Again.for Example, To Construct A Library in Some Other Directory Inc = .. / Misc

Others = .. / Misc / lib.a

$ (Others):

CD $ (inc); make lib.a

BEWARE: HEOUWING WILL NOT WORK (But You'd Think IT Should) Inc = .. / Misc

Others = .. / Misc / lib.a

$ (Others):

CD $ (inc)

Make lib.a

Each Command in The Target Rule IS EXECUTED IN A SEPARATE shell. This makes for some intending constructs and long continuation lines. To generate a tags file srcs = x.c y.c z.c

Ctags = ctags -x> tags

Tags: $ (srcs)

$ {Ctags} $ (srcs)

On Large Projects A Tags File, That Lists All Functions and Their Invocations Is A Handy Tool. To Generate A Listing of Likey Bugs in Your Problems LINT:

LINT $ (CFLAGS) $ (srcs)

Lint is a really good tool for finding those obvious bugs that slip into programs -.. Eg type classes, bad argu- ment list, etc. Some Basic Make Rule People have come to expect certain targets in Makefiles You should always browse first, But it's reasonable to expect what Targets All (or Just Make), Install, And Clean Will Be Found.

make all - should compile everything so that you can do local testing before installing things make install -.. should install things in the right places But watch out that things are installed in the right place for your system make clean -. should clean things up. Get rid of the executables, any temporary files, object files, etc. You may encounter other common targets, some have been already mentioned (tags and lint). An Example Makefile for printenv # make the printenv command #

Owner = bin

Group = bin

Ctags = ctags -x> tags

Cflags = -o

LDFLAGS = -S

CC = CC

Get = co

SRCS = Printenv.c

Objs = printenv.o

Shar = shar

Mandir = / usr / man / manl / printenv.l

BINDIR = / usr / local / bin

Depend = MakeDepend $ (cflags)

All: Printenv

# T Get Things Out of the Revision Control System

$ (Srcs):

$ (GET) $ @

# To make an object from Source

$ (Cc) $ (cflags) -c $ *. C

# To make an executable

Printenv: $ (OBJS)

$ (Cc) $ (ldflags) -o $ @ $ (bjs)

# To install things in the right place

INSTALL: Printenv Printenv.man

$ (Install) -c-o $ (owner) -g $ (group) -m 755 Printenv $ (Bindir)

$ (INSTALL) -C -O $ (OWNER) -G $ (Group) -m 644 Printenv.man $ (MANDIR)

# Where are functions / procedures?

Tags: $ (srcs)

$ (CTAGS) $ (srcs)

# What have I doe wrong?

LINT: $ (srcs)

LINT $ (CFLAGS) $ (srcs)

# What Are the Source Dependencies

Depend: $ (srcs)

$ (Depend) $ (SRCS)

# to make a shar distance

Shar: Clean

$ (Shar) Readme Makefile Printenv.man $ (SRCS)> Shar

# clean out the dross

Clean:

-rm printenv * ~ * .o * .bak core tags shark

# Do not delete this line - make depend depends on it.

Printenv.o: /usr/include/stdio.h

Makefile Implicit Rules Consider The Rule We Used for Printenv Printenv: Printenv.c $ (CFLAGS) Printenv.c $ (ldflags) -o Printenv

We generalized a bit to get Printenv: Printenv.c

$ (Cc) $ (cflags) $ @. C $ (ldflags) -o $ @

The Command IS One That OUGHT TOWORKIT CASES Where We Build An Executable X Out of The Source Code X.c This Can Be Stated As An Implicit Rule: .c:

$ (Cc) $ (cflags) $ @. C $ (ldflags) -o $ @

This Implicit rule says how to make x out of xc -.. Run cc on xc and call the output x The rule is implicit because no particular target is mentioned It can be used in all cases Another common implicit rule is for the construction. Files out of .c (source file). .oc:

$ (Cc) $ (cflags) -c $

Alternative .O.c:

$ (Cc) $ (cflags) -c $ *. C

Make Dependencies It's Pretty Common To Have Source Code That Uses Include Files. For Example:% Cat Program.c

#include

#include "defs.h"

#include "glob.h"

ETC ....

Main (Argc, Argv)

ETC ...

The Implicit Rule Only Covers Part of The Source Code Depen- Dency (It Only Knows That Program). The Usual Method for Handling this is to list the dependen - cies SeparationLy; ETC ...

$ (Cc) $ (cflags) -c $ *. C

ETC ...

Program.o: program.c defs.h Glob.h

Usually an implicit rule and a separate list of dependencies is all you need. And it ought to be easy enough to figure out what the dependencies are. However, there are a number of nice tools around that will automatically generate dependency lists for you. For Example (Trivial): Depend = makedepend $ (cflags)

ETC ...

# What Are the Source Dependencies

Depend: $ (srcs)

$ (Depend) $ (SRCS)

ETC ....

# Do not delete this line - ....

Printenv.o: /usr/include/stdio.hthese Tools (mkdepend, mkmkf, etc.) Are Very Common these Days and aren't Too Difficult To Use or understand. They're Just Shell Scripts That Run CPP (OR CC - M, or etc.) to find out what all the include dependencies are..., One, one ,,,,,,,

Make and makefiles by

REG Quinton

Computing and Communications Services

The University of Western ONTARIO

London, Ontario N6A 5B7

Canada

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

New Post(0)