The makeutility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.

Similar documents
The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.

Reviewing gcc, make, gdb, and Linux Editors 1

Chris' Makefile Tutorial

Intermediate Programming, Spring 2017*

CS11 Intro C++ Spring 2018 Lecture 4

GNU Make 1. 1 material adapted from GNU Make by Richard Stallman

Makefiles SE 2XA3. Term I, 2018/19

2 Compiling a C program

COSC345 Software Engineering. Make

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

CS Basics 15) Compiling a C prog.

Makefile Brief Reference

Lab 2.2. Out: 9 February 2005

CS11 Advanced C++ Fall Lecture 4

[Software Development] Makefiles. Davide Balzarotti. Eurecom Sophia Antipolis, France

Kurt Schmidt. May 23, 2018

Make! CSC230: C and Software Tools. N.C. State Department of Computer Science. Some examples adapted from

CS354 gdb Tutorial Written by Chris Feilbach

Make: a build automation tool

CSE 333 Interlude - make and build tools

Makefile Tutorial. Eric S. Missimer. December 6, 2013

Make: a build automation tool 1/23

CS Students Linux User's Guide

CSE 374 Programming Concepts & Tools

CS 261 Recitation 1 Compiling C on UNIX

Program Development Tools

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

HPC User Environment

The CS-220 Development Environment

Using GNU make C HAPTER 4

Basic Compilation Control with Make

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

Compilation & linkage. Compilation & linkage. Make. Compilation & linkage. Explicit rules. What makefile contains

CS480. Compilers Eclipse, SVN, Makefile examples

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

Starting to Program in C++ (Basics & I/O)

The make utility. Alark Joshi COMPSCI 253

GCC: the GNU Compiler Collection

CS354R: Game Technology

{C} Tools of the Trade

Make was originally a Unix tool from 1976, but it has been re-implemented several times, notably as GNU Make.

Introduction to System Programming : makefile

The Linux Programming Environment. Computer Science Department Texas State University

Maemo Diablo GNU Make and makefiles Training Material

Multiple file project management & Makefile

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

CMPSC 311- Introduction to Systems Programming Module: Build Processing

COMP Lecture Notes The Compiler

CS2141 Software Development using C/C++ Compiling a C++ Program

15-213/18-213/15-513, Spring 2018 C Programming Lab: Assessing Your C Programming Skills

Makefile Practice: Part 1 Making a Single Program without Templated.h Files: Get the MakeBasics folder from the CS260 repository.

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

15213 Recitation Section C

And You Thought There Couldn t be More C++ Fundamentals of Computer Science

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

GCC: the GNU Compiler Collection

CS 403: Lab 6: Profiling and Tuning

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Data and File Structures Laboratory

CS 107 Lecture 18: GCC and Make

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Makefiles are a simple way to organize code compilation. Using a makefile it is possible to compile several source files to produce an executable;

DAY 4. CS3600, Northeastern University. Alan Mislove

Programming in the Large Steps

BMS: Build Management System

C / C++ Coding Rules

CSC 2500: Unix Lab Fall 2016

Laboratory Assignment #3 Eclipse CDT

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

CS240: Programming in C

Systems Programming. laboratory. Compilation automation. make ceedling

CMPT 300. Operating Systems. Brief Intro to UNIX and C

CSE 390 Lecture 8. Large Program Management: Make; Ant

PRINCIPLES OF OPERATING SYSTEMS

/visualc/vcug/_asug_overview.3a_.nmake_reference.htm

Module 3: Working with C/C++

Introduction. Introduction to gcc and Makefiles. Stef Nychka. September 21, Department of Computing Science University of Alberta

cc is really a front-end program to a number of passes or phases of the whole activity of "converting" our C source files to executable programs:

CSCi 4061: Intro to Operating Systems Spring 2017 Instructor: Jon Weissman Assignment 1: Simple Make Due: Feb. 15, 11:55 pm

Software Building (Sestavování aplikací)

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

C++ Lab 07 - Introduction to C++ Build Systems

Reversing. Time to get with the program

Learning Objectives. A Meta Comment. Exercise 1. Contents. From CS61Wiki

Command Line Navigation and Compiling

We first learn one useful option of gcc. Copy the following C source file to your

Using the Unix system. UNIX Introduction

cget Documentation Release Paul Fultz II

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16

CS355 Hw 4. Interface. Due by the end of day Tuesday, March 20.

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Final Topics. Ali Malik

area.o perimeter.o formatting.o geometry.c gcc -o geometry area.o perimeter.o formatting.o geometry.c

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Using HTML Kit to Debug Server-Side Scripts

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

Assignment 5: Priority Queue

Transcription:

What is make? 1 make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss the GNU makeutility included on Linux systems. As the manual* says: The makeutility 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 *http://www.gnu.org/software/make/manual/make.pdf

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

Dependencies 3 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 PlaceUtilities.h: Place.h PlaceQueue.h: Place.h Queue.h PlaceQueueUtilities.h: PlaceQueue.h PlaceQueueUtilities.c: PlaceUtilities.h MarkUp.h We need to understand how the inclusions affect compilation

Dependency Map 4 The C source files exhibit the following dependencies (due to include directives): pqtest.* TestPlace.* TestPlaceQueue.* Markup.* PlaceQueueUtilities.* PlaceUtilities.* PlaceQueue.* Place.* Queue.* We need to understand the dependencies in order to define the rulesmakewill apply.

Makefiles and Rules You use a kind of script called a makefileto tell makewhat to do. A simple makefile is just a list of rules of the form: target : prequisites recipe Prerequisitesare the files that are used as input to create the target. A recipe specifies an action that make carries out. 5

Defining a Simple Rule 6 Here is a simple rule for compiling Queue.c(and so producing Queue.o): target prerequisites Queue.o: Queue.c Queue.h gcc std=c99 Wall -c Queue.c tab!! recipe So, if we invoke makeon this rule, makewill execute the command: gcc std=c99 Wall -c Queue.c which will (ideally) result in the creation of the object file Queue.o.

Defining a More Complex Rule 7 Here is a simple rule for compiling TestPlace.c(and so producing TestPlace.o): '/'tells makethe current element continues on the next line 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 Now, we have some issues: - This doesn t save us any rebuilding every C file that TestPlace.odepends 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.

Using the Dependencies We can specify targets as prerequisites, as well as C source files: 8 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 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, iffthat target has a prerequisite, that has changed since that target was last built

Makefile Variables We can define variables in our makefile and use them in recipes: 9 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). Syntax note: no spaces around '='.

Rules Without Prerequisites 10 We can also define a rule with no prerequisites; the most common use is probably to define a cleanup rule: clean: rm -f *.o *.stackdump Invoking makeon this target would cause the removal of all object and stackdumpfiles from the directory.

A Complete Makefile 11 Here is a complete makefile for the example project: # 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)...

A Complete Makefile 12... # 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...

A Complete Makefile 13... 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

Running make 14 make can be invoked in several ways, including: make make <target> make f <makefile name> <target> In the first two cases, makelooks for a makefile, in the current directory, with a default name. GNU makelooks for the following names, in this order: GNUmakefile makefile Makefile If no target is specified, makewill process the first rule in the makefile.

Examples using make 15 Using the makefile shown above, and the source files indicated earlier: [wdm@vmcentos64 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 [wdm@vmcentos64 Make]$ Since I hadn t compiled anything yet, make invoked all of the rules in Makefile.

Examples using make 16 Now, I ll modify one of the C files and run makeagain: [wdm@vmcentos64 Make]$ touch MarkUp.c [wdm@vmcentos64 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 [wdm@vmcentos64 Make]$ The only recipes that were invoked were those for the targets that depend on MarkUp.c.

Examples using make 17 Now, I ll modify a deeper C file and run makeagain: [wdm@vmcentos64 Make]$ touch Place.c [wdm@vmcentos64 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 [wdm@vmcentos64 Make]$ Again, the only files that were recompiled were the ones depending on the changed file.

Examples using make 18 Of course, we can also build secondary targets: [wdm@vmcentos64 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 [wdm@vmcentos64 Make]$ The only files that were compiled were the ones on which the specified target depends.

Using Implicit Rules 19 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 manual for a full discussion of this.