How to write a Makefile - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

How to write a Makefile

Description:

CC = gcc. This is valid: LIBS = '-lncurses -lm -lsdl' Quote the ... CC = gcc. OBJS = mycode1.o mycode2.o. CFLAGS = -c -O3 -g -Wall. LFLAGS = -O3 -g -Wall ... – PowerPoint PPT presentation

Number of Views:217
Avg rating:3.0/5.0
Slides: 12
Provided by: cseOhi
Category:

less

Transcript and Presenter's Notes

Title: How to write a Makefile


1
How to write a Makefile
  • Based on tutorial at
  • http//www.dirac.org/p/TA/tutorials/makefiles/make
    files.html

2
Introduction
  • Tool used to make it easier to compile
  • Compile only what needs to be compiled
  • Every line of a makefile is one of three types
  • Assignments
  • Target lines
  • Rule lines

3
Assignments
  • By convention, all variables in a makefile are
    uppercase.
  • Spaces are irrelevant around the sign.
  • CC gcc
  • This is valid
  • LIBS "-lncurses -lm -lsdl"
  • Quote the asterisk
  • MYFACE ")"
  • The following line prints the string "gcc"
  • CC gcc
  • echo CC

4
Target Lines
  • Here is an example of a target line with
    dependencies
  • all main.o main.h mycode.o mycode.h
  • and here is a target line without dependencies
  • clean
  • Do not begin with a space/tab

5
Rule Lines
  • Each target line is followed by 0 or more rule
    lines
  • Each rule line must start with a TAB
  • Examples
  • ltTABgt gcc -g -o myfile myfile.c
  • ltTABgt rm -rf core .o
  • ltTABgt echo "hello"

6
Dependencies
  • OBJS mycode.o
  • CC gcc
  • CFLAGS -Wall -O3
  • all OBJS
  • ltTABgt CC CFLAGS OBJS -o mycode
  • mycode.o mycode.c
  • ltTABgt CC -c CFLAGS mycode.c

7
Some points to note
  • make executes the first target
  • Note that the rule to make dependents must come
    AFTER you declare them to be a dependence
  • So following example is incorrect
  • mycode1.o
  • ltTABgt gcc -c -Wall mycode1.c
  • all mycode1.o mycode2.o
  • ltTABgt gcc -Wall mycode1.o mycode2.o -o
    CoolProgram
  • mycode2.o
  • ltTABgt gcc -c -Wall mycode2.c

8
Example
  • CC gcc
  • OBJS mycode1.o mycode2.o
  • CFLAGS -c -O3 -g -Wall
  • LFLAGS -O3 -g -Wall
  • all OBJS
  • ltTABgt CC LFLAGS OBJS -o MultiFileCode
  • mycode1.o mycode1.c
  • ltTABgt CC CFLAGS mycode1.c
  • mycode2.o mycode2.c
  • ltTABgt CC CFLAGS mycode2.o

9
Improved Example
  • CC gcc
  • OBJS mycode1.o mycode2.o
  • CFLAGS -c -O3 -g -Wall
  • LFLAGS -O3 -g -Wall
  • all OBJS
  • ltTABgt CC LFLAGS OBJS -o MultiFileCode
  • .c.o
  • ltTABgt CC CFLAGS lt

10
Another Example
  • EXE animals
  • SRC animals.c foo.c bar.c
  • CC gcc
  • CC_OPTIONS -g -Wall
  • CS678 /home/user/prasun/678
  • INCLUDES -I(CS678)
  • CFLAGS (CC_OPTIONS) (INCLUDES)
  • LIBS -L(CS678) lcs678
  • OBJ (SRC.c.o)
  • (EXE) (OBJ)
  • ltTABgt (CC) (CC_OPTIONS) -o _at_ (OBJ) (LIBS)
  • clean
  • ltTABgt rm -f (EXE) .o a.out core
  • depend Makefile (SRC)
  • ltTABgt makedepend (INCLUDES) (SRC)
  • end of Makefile DO NOT DELETE THIS LINE --
    make depend depends on it.

11
Other pointers
  • Symbols in makefile
  • http//www.informatik.uni-freiburg.de/mai/files/m
    akeReference.html
  • Makefile Tutorial at College of Hawaii
  • http//www.eng.hawaii.edu/Tutor/Make/
  • Makefile Tutorial at Rutgers
  • http//www.hsrl.rutgers.edu/ug/make_help.html
Write a Comment
User Comments (0)
About PowerShow.com