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

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

Intermediate Programming, Spring 2017*

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

COSC345 Software Engineering. Make

CS11 Intro C++ Spring 2018 Lecture 4

Makefiles SE 2XA3. Term I, 2018/19

Makefile Brief Reference

Reviewing gcc, make, gdb, and Linux Editors 1

2 Compiling a C program

Kurt Schmidt. May 23, 2018

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

CS354 gdb Tutorial Written by Chris Feilbach

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

Make: a build automation tool

CSE 333 Interlude - make and build tools

Lab 2.2. Out: 9 February 2005

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

CS Basics 15) Compiling a C prog.

Make: a build automation tool 1/23

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

CS480. Compilers Eclipse, SVN, Makefile examples

CS 261 Recitation 1 Compiling C on UNIX

CS11 Advanced C++ Fall Lecture 4

CS Students Linux User's Guide

CSE 374 Programming Concepts & Tools

Chris' Makefile Tutorial

Maemo Diablo GNU Make and makefiles Training Material

Program Development Tools

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

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

The CS-220 Development Environment

Basic Compilation Control with Make

Using GNU make C HAPTER 4

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

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

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

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

15213 Recitation Section C

GCC: the GNU Compiler Collection

The make utility. Alark Joshi COMPSCI 253

GCC: the GNU Compiler Collection

HPC User Environment

{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

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

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

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

CS 107 Lecture 18: GCC and Make

COMP Lecture Notes The Compiler

Multiple file project management & Makefile

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

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

DAY 4. CS3600, Northeastern University. Alan Mislove

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

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

Programming in the Large Steps

Laboratory Assignment #3 Eclipse CDT

Project Management with RStudio

CS354R: Game Technology

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

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

The Linux Programming Environment. Computer Science Department Texas State University

CMPSC 311- Introduction to Systems Programming Module: Build Processing

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

CMPSC 311- Introduction to Systems Programming Module: Build Processing

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:

Final Topics. Ali Malik

Table Of Contents. 1. Zoo Information a. Logging in b. Transferring files 2. Unix Basics 3. Homework Commands

Using the Unix system. UNIX Introduction

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

BMS: Build Management 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;

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

Compiling Code using Makefiles and GNUstep. 2501ICT Nathan

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Lab #3 Automating Installation & Introduction to Make Due in Lab, September 15, 2004

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

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

Data and File Structures Laboratory

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

Systems Programming. laboratory. Compilation automation. make ceedling

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

Software Building (Sestavování aplikací)

PathFinder-XD for MIPS Powered Devices. Simulator

CS 403: Lab 6: Profiling and Tuning

Reversing. Time to get with the program

Example: intmath lib

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

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

Command Line Navigation and Compiling

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

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Both parts center on the concept of a "mesa", and make use of the following data type:

Follow us on Twitter for important news and Compiling Programs

Using HTML Kit to Debug Server-Side Scripts

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

Projects and Make Files

A Capacity: 10 Usage: 4 Data:

Module 3: Working with C/C++

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 make utility included on Linux systems. As the 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 *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: driver.c Polynomial.h Polynomial.c PolyTester.h PolyTester.c the main driver the "public" interface of the Polynomial type the implementation of the Polynomial type the "public" interface of the test harness the implementation of the test harness The example is derived from an assignment that is occasionally used in CS 2506.

Dependencies 3 The C source files use the following include directives related to files in the project: driver.c: Polynomial.h PolyTester.h PolyTester.h: Polynomial.h Polynomial.c: Polynomial.h PolyTester.c: PolyTester.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): driver PolyTester Polynomial Source file driver.c PolyTester.c Polynomial.c Recompile if changes are made to: driver.c or PolyTester.* or Polynomial.* PolyTester.c or Polynomial.* Polynomial.c

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 : prerequisites recipe Prerequisites are 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 Here is a simple rule for compiling Polynomial.c (and so producing Polynomial.o): 6 target prerequisites Polynomial.o: Polynomial.c Polynomial.h gcc std=c99 Wall -c Polynomial.c tab!! recipe So, if we invoke make on this rule, make will execute the command: gcc std=c99 Wall -c Polynomial.c which will (ideally) result in the creation of the object file Polynomial.o.

Defining a More Complex Rule Here is a simple rule for compiling PolyTester.c (and so producing PolyTester.o): 7 PolyTester.o: Polynomial.c Polynomial.h PolyTester.c PolyTester.h gcc -c -std=c99 -Wall PolyTester.c Polynomial.c Now, we have some issues: - This doesn t save us any rebuilding every C file that PolyTester.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.

Using the Dependencies We can specify targets as prerequisites, as well as C source files: 8 PolyTester.o: Polynomial.o PolyTester.c PolyTester.h gcc -c -std=c99 -Wall PolyTester.c Now, if we invoke make on the target PolyTester.o: - make examines the modification time for each direct (and indirect) prerequisite for PolyTester.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

Makefile Variables We can define variables in our makefile and use them in recipes: 9 CC=gcc CFLAGS=-O0 m64 -std=c99 -Wall -W -ggdb3 PolyTester.o: Polynomial.o PolyTester.c $(CC) $(CFLAGS) -c PolyTester.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 make on this target would cause the removal of all object and stackdump files from the directory.

A Complete Makefile 11 Here is a complete makefile for the example project: # Specify shell to execute recipes 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 # # example makefile -- version 0 # CC=gcc CFLAGS=-O0 -std=c99 -m32 Wall -W -ggdb3...

A Complete Makefile 12 Here is a complete makefile for the example project:... driver: Polynomial.o PolyTester.o $(CC) $(CFLAGS) o driver driver.c Polynomial.o PolyTester.o PolyTester.o: Polynomial.o PolyTester.c $(CC) $(CFLAGS) -c PolyTester.c Polynomial.o: Polynomial.c Polynomial.h $(CC) $(CFLAGS) -c Polynomial.c clean: rm *.o

Running make 13 make can be invoked in several ways, including: make make <target> make f <makefile name> <target> 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.

Examples using make 14 Using the makefile shown above, and the source files indicated earlier: centos > ll total 64 -rw-rw-r--. 1 wdm wdm 1197 Feb 15 21:07 driver.c -rw-rw-r--. 1 wdm wdm 350 Feb 15 21:18 makefile -rw-rw-r--. 1 wdm wdm 10824 Feb 15 21:07 Polynomial.c -rw-rw-r--. 1 wdm wdm 5501 Feb 15 21:07 Polynomial.h -rw-rw-r--. 1 wdm wdm 28914 Feb 15 21:07 PolyTester.c -rw-rw-r--. 1 wdm wdm 886 Feb 15 21:07 PolyTester.h centos > make driver gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -c Polynomial.c gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -c PolyTester.c gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -o driver driver.c Polynomial.o PolyTester.o centos > Since I hadn t compiled anything yet, make invoked all of the rules in makefile.

Examples using make 15 Now, I ll modify one of the C files and run make again: centos > touch PolyTester.c centos > make driver gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -c PolyTester.c gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -o driver driver.c Polynomial.o PolyTester.o centos > The only recipes that were invoked were those for the targets that depend on PolyTester.c.

Examples using make 16 Now, I ll modify a deeper C file and run make again: centos > touch Polynomial.c centos > make driver gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -c Polynomial.c gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -c PolyTester.c gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -o driver driver.c Polynomial.o PolyTester.o centos > Again, the only files that were recompiled were the ones depending on the changed file.

Examples using make 17 Of course, we can also build secondary targets: total 64 -rw-rw-r--. 1 wdm wdm 1197 Feb 15 21:07 driver.c -rw-rw-r--. 1 wdm wdm 350 Feb 15 21:18 makefile -rw-rw-r--. 1 wdm wdm 10824 Feb 15 21:29 Polynomial.c -rw-rw-r--. 1 wdm wdm 5501 Feb 15 21:07 Polynomial.h -rw-rw-r--. 1 wdm wdm 28914 Feb 15 21:27 PolyTester.c -rw-rw-r--. 1 wdm wdm 886 Feb 15 21:07 PolyTester.h centos > make PolyTester.o gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -c Polynomial.c gcc -O0 -std=c99 -m32 -Wall -ggdb3 -W -c PolyTester.c centos > The only files that were compiled were the ones on which the specified target depends.