1 Building Jennifer Rexford. 2 Goals of this Lecture Help you learn about: The build process for multi-file programs Partial builds of multi-file programs.

Slides:



Advertisements
Similar presentations
The make Utility Programming Tools and Environments Winter 2006.
Advertisements

Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
The Makefile utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
1 CSE 390 Lecture 8 Large Program Management: Make; Ant slides created by Marty Stepp, modified by Josh Goodwin
1 The Makefile Utility ABC – Chapter 11,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
Guide To UNIX Using Linux Third Edition
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
1 Building. 2 Goals of this Lecture Help you learn about: The build process for multi-file programs Partial builds of multi-file programs make, a popular.
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
Makefiles, and.h files, and.c files, and.o files, OH MY! For projects with more complexity. (Great.. Just what we needed)
Introduction Use of makefiles to manage the build process Declarative, imperative and relational rules Environment variables, phony targets, automatic.
리눅스 : Lecture 5 UNIX 유틸리티 : text editor, compilation (make), …
Jump to first page (C) 1998, Arun Lakhotia 1 Software Configuration Management: Build Control Arun Lakhotia University of Southwestern Louisiana Po Box.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Makefile M.A Doman. Compiling multiple objects Card.cpp -> Card.o Deck.cpp -> Deck.o main.cpp -> main.o main.o Deck.o Card.o -> Dealer.exe.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Separate Compilation make and makefiles
CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.
Introduction to Systems Programming (CS 0449) C Preprocessing Makefile File I/O.
C Programming Separate Compilation Variable Lifetime and Scope make.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Make: File Dependency System Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
Make Make is a system utility that automatically compiles your programs for you Make looks for a file named Makefile (or makefile) in the current directory.
Multiple File Compilation and linking By Bhumik Sapara.
CSI605 Introduction to make. Advantages of Make Significantly reduces the amount of time spent compiling a program. Insures that programs are compiled.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Makefile Script file to automate program compilation and linking (making) 1. Write the "makefile" 2. Write your programs 3. Run "make" or "make -f makefile"
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Multiple file project management & Makefile
Precept 10 : Building and Performance
The make utility (original presentation courtesy of Alark Joshi)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Brief Intro to Make CST494/ Gannod.
Makefiles Caryl Rahn.
SCMP Special Topic: Software Development Spring 2017 James Skon
Editor, Compiler, Linker, Debugger, Makefiles
Introduction to C Topics Compilation Using the gcc Compiler
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Makefiles and the make utility
CSC 253 Lecture 6.
Data Structures and Programming Techniques
CMPSC 60: Week 4 Discussion
CMSC 202 Additional Lecture – Makefiles
Kyle Fitzpatrick Konstantin Zak 11/29/2004
SCMP Software Development Spring 2018 James Skon
Appendix F C Programming Environment on UNIX Systems
Makefiles and the make utility
Compiler vs linker The compiler translates one .c file into a .o file
SCMP Software Development Spring 2018 James Skon
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
The make utility (original presentation courtesy of Alark Joshi)
Presentation transcript:

1 Building Jennifer Rexford

2 Goals of this Lecture Help you learn about: The build process for multi-file programs Partial builds of multi-file programs make, a popular tool for automating (partial) builds Why? A complete build of a large multi-file program typically consumes many hours To save build time, a power programmer knows how to do partial builds A power programmer knows how to automate (partial) builds using make

Review: Multi-File Programs #ifndef INTMATH_INCLUDED #define INTMATH_INCLUDED int gcd(int i, int j); int lcm(int i, int j); #endif #include "intmath.h" int gcd(int i, int j) { int temp; while (j != 0) { temp = i % j; i = j; j = temp; } return i; } int lcm(int i, int j) { return (i / gcd(i, j)) * j; } #include "intmath.h" #include int main(void) { int i; int j; printf("Enter the first integer:\n"); scanf("%d", &i); printf("Enter the second integer:\n"); scanf("%d", &j); printf("Greatest common divisor: %d.\n", gcd(i, j)); printf("Least common multiple: %d.\n", lcm(i, j); return 0; } intmath.h (interface) intmath.c (implementation) testintmath.c (client) Note: intmath.h is #included into intmath.c and testintmath.c See precept handouts for stylistically better version 3

Review: Multi-File Programs testintmath.c gcc217 –E testintmath.c > testintmath.i intmath.c intmath.h testintmath.iintmath.i testintmath.sintmath.s testintmath.o intmath.o testintmath gcc217 –E intmath.c > intmath.i gcc217 –S testintmath.igcc217 –S intmath.i gcc217 –c testintmath.sgcc217 –c intmath.s gcc217 testintmath.o intmath.o –o testintmath libc.a Preprocess Compile Assemble Link 4

Agenda Motivation for Make Make Fundamentals Non-File Targets Macros Abbreviations Pattern Rules 5

6 Motivation for Make (Part 1) Building testintmath, approach 1: Use one gcc217 command to preprocess, compile, assemble, and link intmath.hintmath.c testintmath.c testintmath gcc217 testintmath.c intmath.c –o testintmath

7 gcc217 –c intmath.cgcc217 –c testintmath.c Motivation for Make (Part 2) Building testintmath, approach 2: Preprocess, compile, assemble to produce.o files Link to produce executable binary file Recall: -c option tells gcc217 to omit link intmath.hintmath.c testintmath.c testintmath testintmath.ointmath.o gcc217 testintmath.o intmath.o –o testintmath

8 Partial Builds Approach 2 allows for partial builds Example: Change intmath.c Must rebuild intmath.o and testintmath Need not rebuild testintmath.o !!! intmath.hintmath.c testintmath.c testintmath testintmath.ointmath.o gcc217 –c testintmath.cgcc217 –c intmath.c changed gcc217 testintmath.o intmath.o –o testintmath

9 Partial Builds Example: Change testintmath.c Must rebuild testintmath.o and testintmath Need not rebuild intmath.o !!! If program contains many.c files, could save many hours of build time intmath.hintmath.c testintmath.c testintmath testintmath.ointmath.o gcc217 testintmath.o intmath.o –o testintmath gcc217 –c testintmath.cgcc217 –c intmath.c changed

10 Partial Builds However, changing a.h file can be more dramatic Example: Change intmath.h intmath.h is #included into testintmath.c and intmath.c Changing intmath.h effectively changes testintmath.c and intmath.c Must rebuild testintmath.o, intmath.o, and testintmath intmath.hintmath.c testintmath.c testintmath testintmath.ointmath.o gcc217 testintmath.o intmath.o –o testintmath gcc217 –c testintmath.cgcc217 –c intmath.c changed

11 Wouldn’t It Be Nice… Observation Doing partial builds manually is tedious and error-prone Wouldn’t it be nice if there were a tool How would the tool work? Input: Dependency graph (as shown previously) Specifies file dependencies Specifies commands to build each file from its dependents Date/time stamps of files Algorithm: If file B depends on A and date/time stamp of A is newer than date/time stamp of B, then rebuild B using the specified command

Agenda Motivation for Make Make Fundamentals Non-File Targets Macros Abbreviations Pattern Rules 12

The Make Tool Who? Stuart Feldman When? 1976 Where? Bell Labs Why? Automate partial builds 13

14 Make Command Syntax Command syntax make [-f makefile] [target] makefile Textual representation of dependency graph Contains dependency rules Default name is makefile, then Makefile target What make should build Usually:.o file, or an executable binary file Default is first one defined in makefile

15 Dependency Rules Dependency rule syntax target: dependencies command target : the file you want to build dependencies : the files on which the target depends command : what to execute to create the target (after a TAB character) Dependency rule semantics Build target iff it is older than any of its dependencies Use command to do the build Work recursively; examples illustrate…

16 Makefile Version 1 testintmath: testintmath.o intmath.o gcc217 testintmath.o intmath.o –o testintmath testintmath.o: testintmath.c intmath.h gcc217 -c testintmath.c intmath.o: intmath.c intmath.h gcc217 -c intmath.c intmath.h intmath.c testintmath.c testintmath testintmath.ointmath.o gcc217 testintmath.o intmath.o –o testintmath gcc217 –c testintmath.cgcc217 –c intmath.c Makefile:

17 Version 1 in Action $ make testintmath gcc217 -c testintmath.c gcc217 -c intmath.c gcc217 testintmath.o intmath.o -o testintmath $ touch intmath.c $ make testintmath gcc217 -c intmath.c gcc217 testintmath.o intmath.o -o testintmath $ make testintmath make: `testintmath' is up to date. $ make make: `testintmath' is up to date. At first, to build testintmath make issues all three gcc commands Use the touch command to change the date/time stamp of intmath.c make does a partial build make notes that the specified target is up to date The default target is testintmath, the target of the first dependency rule

Agenda Motivation for Make Make Fundamentals Non-File Targets Macros Abbreviations Pattern Rules 18

19 Non-File Targets Adding useful shortcuts for the programmer make all : create the final executable binary file make clean : delete all.o files, executable binary file make clobber : delete all Emacs backup files, all.o files, executable binary file Commands in the example rm –f : remove files without querying the user Files ending in ‘ ~ ’ and starting/ending in ‘ # ’ are Emacs backup files all: testintmath clobber: clean rm -f *~ \#*\# clean: rm -f testintmath *.o

20 Makefile Version 2 # Dependency rules for non-file targets all: testintmath clobber: clean rm -f *~ \#*\# clean: rm -f testintmath *.o # Dependency rules for file targets testintmath: testintmath.o intmath.o gcc217 testintmath.o intmath.o –o testintmath testintmath.o: testintmath.c intmath.h gcc217 -c testintmath.c intmath.o: intmath.c intmath.h gcc217 -c intmath.c

21 Version 2 in Action $ make clean rm -f testintmath *.o $ make clobber rm -f testintmath *.o rm -f *~ \#*\# $ make all gcc217 -c testintmath.c gcc217 -c intmath.c gcc217 testintmath.o intmath.o -o testintmath $ make make: Nothing to be done for `all'. make observes that “clean” target doesn’t exist; attempts to build it by issuing “rm” command Same idea here, but “clobber” depends upon “clean” “all” depends upon “testintmath” “all” is the default target

Agenda Motivation for Make Make Fundamentals Non-File Targets Macros Abbreviations Pattern Rules 22

23 Macros make has a macro facility Performs textual substitution Similar to C preprocessor’s #define Macro definition syntax macroname = macrodefinition make replaces $(macroname) with macrodefinition in remainder of Makefile Example: Make it easy to change build commands CC = gcc217 Example: Make it easy to change build flags CFLAGS = -D NDEBUG –O

24 Makefile Version 3 # Macros CC = gcc217 # CC = gcc217m CFLAGS = # CFLAGS = -g # CFLAGS = -D NDEBUG # CFLAGS = -D NDEBUG -O # Dependency rules for non-file targets all: testintmath clobber: clean rm -f *~ \#*\# clean: rm -f testintmath *.o # Dependency rules for file targets testintmath: testintmath.o intmath.o $(CC) $(CFLAGS) testintmath.o intmath.o -o testintmath testintmath.o: testintmath.c intmath.h $(CC) $(CFLAGS) -c testintmath.c intmath.o: intmath.c intmath.h $(CC) $(CFLAGS) -c intmath.c

25 Version 3 in Action Same as Version 2

Agenda Motivation for Make Make Fundamentals Non-File Targets Macros Abbreviations Pattern Rules 26

27 Abbreviations Target file: First item in the dependency list: $< Example testintmath: testintmath.o intmath.o $(CC) $(CFLAGS) testintmath.o intmath.o –o testintmath testintmath: testintmath.o intmath.o $(CC) $(CFLAGS) $< intmath.o –o

28 Makefile Version 4 # Macros CC = gcc217 # CC = gcc217m CFLAGS = # CFLAGS = -g # CFLAGS = -D NDEBUG # CFLAGS = -D NDEBUG -O # Dependency rules for non-file targets all: testintmath clobber: clean rm -f *~ \#*\# clean: rm -f testintmath *.o # Dependency rules for file targets testintmath: testintmath.o intmath.o $(CC) $(CFLAGS) $< intmath.o -o testintmath.o: testintmath.c intmath.h $(CC) $(CFLAGS) -c $< intmath.o: intmath.c intmath.h $(CC) $(CFLAGS) -c $<

29 Version 4 in Action Same as Version 2

Agenda Motivation for Make Make Fundamentals Non-File Targets Macros Abbreviations Pattern Rules 30

31 Pattern Rules Pattern rule Wildcard version of dependency rule Example: Translation: To build a.o file from a.c file of the same name, use the command $(CC) $(CFLAGS) -c $< With pattern rule, dependency rules become simpler: %.o: %.c $(CC) $(CFLAGS) -c $< testintmath: testintmath.o intmath.o $(CC) $(CFLAGS) $< intmath.o –o testintmath.o: testintmath.c intmath.h intmath.o: intmath.c intmath.h Can omit build command

32 Pattern Rules Bonus Bonus with pattern rules First dependency is assumed testintmath: testintmath.o intmath.o $(CC) $(CFLAGS) $< intmath.o –o testintmath.o: intmath.h intmath.o: intmath.h Can omit first dependency testintmath: testintmath.o intmath.o $(CC) $(CFLAGS) $< intmath.o –o testintmath.o: testintmath.c intmath.h intmath.o: intmath.c intmath.h

33 Makefile Version 5 # Macros CC = gcc217 # CC = gcc217m CFLAGS = # CFLAGS = -g # CFLAGS = -D NDEBUG # CFLAGS = -D NDEBUG -O # Pattern rule %.o: %.c $(CC) $(CFLAGS) -c $< # Dependency rules for non-file targets all: testintmath clobber: clean rm -f *~ \#*\# clean: rm -f testintmath *.o # Dependency rules for file targets testintmath: testintmath.o intmath.o $(CC) $(CFLAGS) $< intmath.o -o testintmath.o: intmath.h intmath.o: intmath.h

34 Version 5 in Action Same as Version 2

Makefile Guidelines In a proper Makefile, each object file: Depends upon its.c file Does not depend upon any other.c file Does not depend upon any.o file Depends upon any.h file that its.c file #includes directly or indirectly c.h d.h a.h a.o a.cb.c b.o x 35 a.o: a.c a.h c.h d.h gcc217 –c a.c

36 Makefile Guidelines In a proper Makefile, each executable binary file: Depends upon the.o files that comprise it Does not depend upon any.c files Does not depend upon any.h files c.h d.h a.h a.o a.cb.c b.o x x: a.o b.o gcc217 a.o b.o –o x

37 Making Makefiles In this course Create Makefiles manually Beyond this course Can use tools to generate Makefiles See mkmf, others

38 Makefile Gotchas Beware: Each command (i.e., second line of each dependency rule) must begin with a tab character, not spaces Use the rm –f command with caution

39 Make Resources C Programming: A Modern Approach (King) Section 15.4 GNU make

Summary Motivation for Make Automation of partial builds Make fundamentals (Makefile version 1) Dependency rules, targets, dependencies, commands Non-file targets (Makefile version 2) Macros (Makefile version 3) Abbreviations (Makefile version 4) Pattern rules (Makefile version 5) 40