Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Makefiles  Provide a way for separate compilation.  Describe the dependencies among the project files.  The make utility.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
Compilation & linkage.h read.h.c read.c.c main.c.c list.c.h list.h prog1 Linkage: g++ read.o main.o list.o –o prog1.o main.o.o list.o.o read.o Compilation:
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.
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.
26-Jun-15 Rake. rake and make A program can consist of many source code files This is always true in Rails! The files may need to be compiled in a certain.
Basic linux shell commands and Makefiles. Log on to engsoft.rutgers.edu Open SSH Secure Shell – Quick Connect Hostname: engsoft.rutgers.edu Username/password:
Guide To UNIX Using Linux Third Edition
CS465 - Unix C Programming (cc/make and configuration control)
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.
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.
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. 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 make is a UNIX utility for building projects that are comprised of multiple source filesmake is a UNIX utility for building projects that.
Introduction Use of makefiles to manage the build process Declarative, imperative and relational rules Environment variables, phony targets, automatic.
Makefile Introduction Jia – Wei Lin 1. Outline Why we use make ? Create a Description File Rules of Makefile How make Processes a Makefile? GCC Flags.
Linux Operations and Administration
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).
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.
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.
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.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
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
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
The make utility (original presentation courtesy of Alark Joshi)
CSE 303 Lecture 17 Makefiles reading: Programming in C Ch. 15
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Brief Intro to Make CST494/ Gannod.
Makefiles Caryl Rahn.
SEEM3460 Tutorial The Make Utility.
SCMP Special Topic: Software Development Spring 2017 James Skon
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
Rake 4-Dec-18.
Data Structures and Programming Techniques
Kyle Fitzpatrick Konstantin Zak 11/29/2004
SCMP Software Development Spring 2018 James Skon
CSc 352: Elementary “make”
GNU Make.
CSc 352 An Introduction to make
Homework K&R chapter 4.
Makefiles and the make utility
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:

Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1

The Utility make The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them. It is useful when we write large programs that are contained in more than one file. Data Structures and Programming Techniques 2

Preparing and Running make In order to use make, you should create a file named Makefile. This file describes the relationships among files in your program and provides commands for updating each file. In a program, typically, the executable file is updated from object files, which in turn are made by compiling source files. Once a suitable makefile exists, each time you change some source files, the simple shell command make suffices to perform all necessary recompilations. The make program uses the makefile in the current directory and the last-modification times of the files to decide which of the files need to be updated. For each of those files, it issues the recipes recorded in the makefile. Data Structures and Programming Techniques 3

Rules A simple makefile consists of rules with the following syntax: target … : prerequisites … recipe … A target or a goal is usually the name of a file that is generated by a program. Examples of targets are executable or object files. A target can also be the name of an action to carry out, such as ‘clean’. A prerequisite is a file that is used as input to create the target. A target often depends on several files. A recipe is an action that make carries out. A recipe may have more than one command. Prerequisites are optional. For example, the rule containing the delete command associated with the target ‘clean’ does not have prerequisites. A rule explains how and when to remake certain files which are the targets of the particular rule. make carries out the recipe on the prerequisites to create or update the target. Data Structures and Programming Techniques 4

Example pqsort : sorting.o PQImplementation.o gcc sorting.o PQImplementation.o -o pqsort sorting.o :sorting.c PQInterface.h PQTypes.h gcc -c sorting.c PQImplementation.o :PQImplementation.c PQInterface.h PQTypes.h gcc -c PQImplementation.c clean: rm pqsort sorting.o PQImplementation.o Data Structures and Programming Techniques 5

Comments In this example makefile, the targets include the executable file pqsort, and the object files sorting.o and PQImplementation.o. The prerequisites are files such as sorting.c and PQInterface.h and PQTypes.h Recipes include the commands gcc -c sorting.c and gcc -c PQImplementation.c A recipe may follow each line that contains a target and prerequisites. These recipes say how to update the target file. Important: A tab character must come at the beginning of every line that contains a recipe to distinguish recipes from other lines in the makefile. The target clean is not a file, but merely the name of an action. Notice that clean is not a prerequisite of any other rule. Consequently, make never does anything with it unless you tell it specifically. Note also that the rule for clean does not have any prerequisites, so the only purpose of the rule is to run the specified recipe. Targets that do not refer to files but are just actions are called phony targets. Data Structures and Programming Techniques 6

How make Processes a Makefile When make is called, it reads the makefile in the current directory and starts processing the first rule of the makefile. In our case, this is the rule for the executable file pqsort. However, before make can process this rule, it should process the rules that update the files on which pqsort depends i.e., sorting.o and PQImplementation.o. These in turn depend on files such as sorting.c, PQInterface.h and PQTypes.h which are not the targets of any rule so the recursion stops here. Data Structures and Programming Techniques 7

Variables Variables allow a text string to be defined once and substituted in multiple places later. For example, it is standard practice for every makefile to have a variable named objects, which is defined to be a list of all object file names. We can define this variable by writing objects=sorting.o PQImplementation.o Then the variable can be used in the makefile using the notation $(variable). Data Structures and Programming Techniques 8

Example (cont’d) objects=sorting.o PQImplementation.o pqsort : $(objects) gcc $(objects) -o pqsort sorting.o :sorting.c PQInterface.h PQTypes.h gcc -c sorting.c PQImplementation.o :PQImplementation.c PQInterface.h PQTypes.h gcc -c PQImplementation.c clean: rm pqsort $(objects) Data Structures and Programming Techniques 9

Letting make Deduce the Recipes It is not necessary to spell out the recipes for compiling the individual C source files, because make can figure them out. make has an implicit rule for updating a.o file from a correspondingly named.c file using a cc -c command (not gcc ). So we can write our example as follows. Data Structures and Programming Techniques 10

Example (cont’d) objects=sorting.o PQImplementation.o pqsort : $(objects) gcc $(objects) -o pqsort sorting.o : PQInterface.h PQTypes.h PQImplementation.o : PQInterface.h PQTypes.h clean: rm pqsort $(objects) Data Structures and Programming Techniques 11

Rules for Cleaning the Directory We can use makefiles to do other things except compiling programs. For example, we can have a recipe that deletes all the object files and executables so that the directory is clean. In our example, this is done by the following rule: clean: rm pqsort $(objects) clean here is called a phony target. To avoid problems with files with the name clean in the same directory, you can write the above rule as follows:.PHONY clean clean: rm pqsort $(objects) Data Structures and Programming Techniques 12

Rules for Cleaning the Directory (cont’d) You can execute the above rule by executing the shell command make clean Data Structures and Programming Techniques 13

Readings These slides were created by copying (sometimes verbatim!) material from the manual make.html. make.html Read this manual for more information (just reading Chapter 2 will suffice). Data Structures and Programming Techniques 14