GNU Make Computer Organization II 1 ©2005-2013 McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).

Slides:



Advertisements
Similar presentations
Agenda Definitions Evolution of Programming Languages and Personal Computers The C Language.
Advertisements

Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files.
The make Utility Programming Tools and Environments Winter 2006.
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.
CS201 – Makefile Tutorial. A Trivial Makefile # Trivial Makefile for puzzle1.c # Ray S. Babcock, CS201, MSU-Bozeman # 1/5/05 # puzzle1: puzzle1.c gcc.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Systems Dev. Tutorial II: Make, utilities, & scripting Recitation Wednesday, Sept 13 th, 2006.
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
1 CS 201 Makefile Debzani Deb. 2 Remember this? 3 What is a Makefile? A Makefile is a collection of instructions that is used to compile your program.
CS Lecture 11 Outline Compiling C programs using gcc Archiving modules Using Makefiles Debugging using gdb Assignment 3 discussion Lecture 111CS.
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.
G++ and make Dan Wilson CS193 02/01/06. The g++ Compiler What happens when you call g++ to build your program? Phase 1, Compilation:.cpp files are compiled.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
Introduction to The Linaro Toolchain Embedded Processors Training Multicore Software Applications Literature Number: SPRPXXX 1.
1 Introduction to Tool chains. 2 Tool chain for the Sitara Family (but it is true for other ARM based devices as well) A tool chain is a collection of.
Make: the good, the bad, and the ugly Dan Berger Titus Winters
Unix Makefiles COP 3330 Lecture Notes Dr. David A. Gaitros.
July 29, 2003Serguei Mokhov, 1 Makefile Brief Reference COMP 229, 346, 444, 5201 Revision 1.2 Date: July 18, 2004.
Makefiles CISC/QCSE 810. BeamApp and Tests in C++ 5 source code files After any modification, changed source needs to be recompiled all object files need.
Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.cpp.
Introduction Use of makefiles to manage the build process Declarative, imperative and relational rules Environment variables, phony targets, automatic.
August 7, 2003Serguei A. Mokhov, 1 gcc Tutorial COMP 444/5201 Revision 1.1 Date: January 25, 2004.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
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.
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
Setting up Cygwin Computer Organization I 1 May 2010 ©2010 McQuain Cygwin: getting the setup tool Free, almost complete UNIX environment emulation.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Data Display Debugger (DDD)
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015 Makefile Tutorial CIS5027.
Lecture 8  make. Using make for compilation  With medium to large software projects containing many files, it’s difficult to: Type commands to compile.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
Multiple File Compilation and linking By Bhumik Sapara.
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects can become huge My research consists of 1600.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Makefiles CSSE 332 Operating Systems
Cygwin: getting the setup tool
Automating Builds with Makefiles
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Brief Intro to Make CST494/ Gannod.
Makefiles Caryl Rahn.
GCC: the GNU Compiler Collection
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: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Makefiles and the make utility
Makefiles and Notes on Programming Assignment PA2
Data Structures and Programming Techniques
Unix Programming Environment
CMPSC 60: Week 4 Discussion
Kyle Fitzpatrick Konstantin Zak 11/29/2004
Writing Large Programs
Appendix F C Programming Environment on UNIX Systems
CSc 352: Elementary “make”
Cygwin: getting the setup tool
CSc 352 An Introduction to make
Makefiles and the make utility
Debugging.
GCC: the GNU Compiler Collection
SPL – PS1 Introduction to C++.
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:

GNU Make Computer Organization II 1 © McQuain 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 GNU make utility included on Linux systems. As the GNU Make manual* says: The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them. * Using make yields a number of benefits, including: -faster builds for large systems, since only modules that must be recompiled will be -the ability to provide a simple way to distribute build instructions for a project -the ability to provide automated cleanup instructions

GNU Make Computer Organization II 2 © McQuain Source Base The following presentation is based upon the following collection of C source files: pqtest.c the main “driver” TestPlace.h test harness for the Place type TestPlace.c TestPlaceQueue.h test harness for the PlaceQueue type TestPlaceQueue.c MarkUp.h point annotation code used in automated testing MarkUp.c PlaceUtilities.h display code for Place and PlaceQueue objects PlaceUtilities.c PlaceQueueUtilities.h PlaceQueueUtilities.c Place.h structured type encapsulating GIS information Place.c PlaceQueue.h interface for managing a queue of Place objects PlaceQueue.c Queue.h generic queue implementation Queue.c The example is derived from a programming assignment used in CS 2506 during Fall 2013.

GNU Make Computer Organization II 3 © McQuain Dependencies The C source files use the following include directives related to files in the project: pqtest.h: TestPlace.h TestPlaceQueue.h TestPlace.h: Place.h TestPlace.c: PlaceUtilities.h MarkUp.h TestPlaceQueue.c: PlaceQueueUtilities.h PlaceUtilities.h MarkUp.h PlaceQueue.h: Place.h Queue.h PlaceUtilities.h: Place.h We need to understand how the inclusions affect compilation… PlaceQueueUtilities.h: PlaceQueue.h PlaceQueueUtilities.c: PlaceUtilities.h MarkUp.h

GNU Make Computer Organization II 4 © McQuain Dependency Map The C source files exhibit the following dependencies (due to include directives): pqtest.* TestPlace.* TestPlaceQueue.* PlaceQueue.* PlaceUtilities.* Markup.* Place.* Queue.* We need to understand the dependencies in order to define the rules make will apply. PlaceQueueUtilities.*

GNU Make Computer Organization II 5 © McQuain Makefiles and Rules You use a kind of script called a makefile to tell make what to do. A simple makefile is just a list of rules of the form: target … : prequisites … recipe … Prerequisites are the files that are used as input to create the target. A recipe specifies an action that make carries out.

GNU Make Computer Organization II 6 © McQuain Defining a Simple Rule Here is a simple rule for compiling Queue.c (and so producing Queue.o ): Queue.o: Queue.c Queue.h gcc –std=c99 –Wall -c Queue.c target prerequisites recipe So, if we invoke make on this rule, make will execute the command: gcc –std=c99 –Wall -c Queue.c tab!!

GNU Make Computer Organization II 7 © McQuain Defining a More Complex Rule Here is a simple rule for compiling TestPlace.c (and so producing TestPlace.o ): Now, we have some issues: -This doesn’t save us any rebuilding… every C file that TestPlace.o depends on will be recompiled every time we invoke the rule for that target. -There is a lot of redundancy in the statement of the rule… too much typing! -What if we wanted to build for debugging? We’d need to add something (for instance, –ggdb3 ) to the recipe in every rule. That’s inefficient. TestPlace.o: TestPlace.c TestPlace.h \ MarkUp.c MarkUp.h \ PlaceUtilities.c PlaceUtilities.h \ Place.c Place.h gcc –std=c99 -c TestPlace.c MarkUp.c PlaceUtilities.c Place.c

GNU Make Computer Organization II 8 © McQuain Using the Dependencies We can specify targets as prerequisites, as well as C source files: Now, if we invoke make on the target TestPlaceQueue.o : -make examines the modification time for each direct and indirect prerequisite for TestPlace.o -each involved target is rebuilt, by invoking its recipe, iff that target has a prerequisite, that has changed since that target was last built TestPlace.o: TestPlace.c TestPlace.h PlaceUtilities.o MarkUp.o gcc –std=c99 -c TestPlace.c PlaceUtilities.o: PlaceUtilities.c PlaceUtilities.h Place.o gcc –std=c99 -c PlaceUtilities.c MarkUp.o: MarkUp.c MarkUp.h gcc –std=c99 -c MarkUp.c

GNU Make Computer Organization II 9 © McQuain Makefile Variables We can define variables in our makefile and use them in recipes: CC=gcc CFLAGS=-O0 -m32 -std=c99 -Wall -W -ggdb3 TestPlace.o: TestPlace.c TestPlace.h PlaceUtilities.o MarkUp.o $(CC) $(CFLAGS) -c TestPlace.c This would make it easier to alter the compiler options for all targets (or to change compilers).

GNU Make Computer Organization II 10 © McQuain Rules Without Prerequisites clean: rm -f *.o *.stackdump We can also define a rule with no prerequisites; the most common use is probably to define a cleanup rule: Invoking make on this target would cause the removal of all object and stackdump files from the directory.

GNU Make Computer Organization II 11 © McQuain A Complete Makefile # Makefile for assignment C3, CS 2506, Fall 2013 # SHELL=/bin/bash # Set compilation options: # # -O0 no optimizations; remove after debugging # -m32 create 32-bit executable # -std=c99 use C99 Standard features # -Wall show "all" warnings # -W show even more warnings (annoying) # -ggdb3 add extra debug info; remove after debugging # CC=gcc CFLAGS=-O0 -m32 -std=c99 -Wall -W -ggdb3 OBJECTS = pqtest.o Place.o PlaceQueue.o Queue.o TestPlace.o \ TestPlaceQueue.o MarkUp.o PlaceUtilities.o \ PlaceQueueUtilities.o # Build the test code (full project): pqtest: $(OBJECTS) $(CC) $(CFLAGS) -o pqtest $(OBJECTS)... Here is a complete makefile for the example project:

GNU Make Computer Organization II 12 © McQuain A Complete Makefile... # Rules for components: pqtest.o: pqtest.c TestPlace.o TestPlaceQueue.o $(CC) $(CFLAGS) -c pqtest.c Place.o: Place.c Place.h $(CC) $(CFLAGS) -c Place.c PlaceQueue.o: PlaceQueue.c PlaceQueue.h Place.o Queue.o $(CC) $(CFLAGS) -c PlaceQueue.c Queue.o: Queue.c Queue.h $(CC) $(CFLAGS) -c Queue.c TestPlace.o: TestPlace.c TestPlace.h PlaceUtilities.o MarkUp.o $(CC) $(CFLAGS) -c TestPlace.c TestPlaceQueue.o: TestPlaceQueue.c TestPlaceQueue.h \ PlaceQueueUtilities.o MarkUp.o $(CC) $(CFLAGS) -c TestPlaceQueue.c PlaceUtilities.o: PlaceUtilities.c PlaceUtilities.h Place.o $(CC) $(CFLAGS) -c PlaceUtilities.c...

GNU Make Computer Organization II 13 © McQuain A Complete Makefile... PlaceUtilities.o: PlaceUtilities.c PlaceUtilities.h Place.o $(CC) $(CFLAGS) -c PlaceUtilities.c PlaceQueueUtilities.o: PlaceQueueUtilities.c \ PlaceQueueUtilities.h \ PlaceQueue.o PlaceUtilities.o $(CC) $(CFLAGS) -c PlaceQueueUtilities.c MarkUp.o: MarkUp.c MarkUp.h $(CC) $(CFLAGS) -c MarkUp.c # Cleaning rules: clean: rm -f *.o *.stackdump cleantext: rm -f *.txt cleanallfiles: rm -f *.o *.txt

GNU Make Computer Organization II 14 © McQuain Running make make can be invoked in several ways, including: make make –f In the first two cases, make looks for a makefile, in the current directory, with a default name. GNU make looks for the following names, in this order: GNUmakefile makefile Makefile If no target is specified, make will process the first rule in the makefile.

GNU Make Computer Organization II 15 © McQuain Examples using make Using the makefile shown above, and the source files indicated earlier: Make]$ make pqtest gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c Place.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c PlaceUtilities.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c MarkUp.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c TestPlace.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c Queue.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c PlaceQueue.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c PlaceQueueUtilities.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c TestPlaceQueue.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c pqtest.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -o pqtest pqtest.o Place.o PlaceQueue.o Queue.o TestPlace.o TestPlaceQueue.o MarkUp.o PlaceUtilities.o PlaceQueueUtilities.o Make]$ Since I hadn’t compiled anything yet, make invoked all of the rules in Makefile.

GNU Make Computer Organization II 16 © McQuain Examples using make Now, I’ll modify one of the C files and run make again: Make]$ touch MarkUp.c Make]$ make pqtest gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c MarkUp.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c TestPlace.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c TestPlaceQueue.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c pqtest.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -o pqtest pqtest.o Place.o PlaceQueue.o Queue.o TestPlace.o TestPlaceQueue.o MarkUp.o PlaceUtilities.o PlaceQueueUtilities.o Make]$ The only recipes that were invoked were those for the targets that depend on MarkUp.c.

GNU Make Computer Organization II 17 © McQuain Examples using make Now, I’ll modify a “deeper” C file and run make again: Make]$ touch Place.c Make]$ make pqtest gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c Place.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c PlaceUtilities.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c TestPlace.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c PlaceQueue.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c PlaceQueueUtilities.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c TestPlaceQueue.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c pqtest.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -o pqtest pqtest.o Place.o PlaceQueue.o Queue.o TestPlace.o TestPlaceQueue.o MarkUp.o PlaceUtilities.o PlaceQueueUtilities.o Make]$ Again, the only files that were recompiled were the ones depending on the changed file.

GNU Make Computer Organization II 18 © McQuain Examples using make Of course, we can also build “secondary” targets: Make]$ make PlaceQueue.o gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c Place.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c Queue.c gcc -O0 -m32 -std=c99 -Wall -W -ggdb3 -c PlaceQueue.c Make]$ The only files that were compiled were the ones on which the specified target depends.

GNU Make Computer Organization II 19 © McQuain Using Implicit Rules Finally, we can simplify the makefile by taking advantage of the fact that make applies certain implicit rules by default:... # Build components: pqtest.o: Place.o TestPlace.o TestPlaceQueue.o Place.o: Place.h PlaceQueue.o: PlaceQueue.h Place.o Queue.o Queue.o: Queue.h TestPlace.o: TestPlace.h PlaceUtilities.o MarkUp.o TestPlaceQueue.o: TestPlaceQueue.h PlaceQueueUtilities.o MarkUp.o PlaceUtilities.o: PlaceUtilities.h Place.o PlaceQueueUtilities.o: PlaceQueueUtilities.h PlaceQueue.o PlaceUtilities.o MarkUp.o: MarkUp.h... See the GNU Make manual for a full discussion of this.