Unix Makefiles COP 3330 Lecture Notes Dr. David A. Gaitros.

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.
Make. A simple make file $ make program Strength of make is its sensitivity to dependency hierarchies. Specify such dependencies in a description file.
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.
Command Line arguments Main function: int main(argc, char *argv[]) –argc is the count of command line arguments –argv is an array of strings argv[0] is.
The Makefile utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
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.
CS465 - Unix C Programming (cc/make and configuration control)
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
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.
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction What is Unix? An operation system (OS), similar to Windows, MacOS X Why learn Unix? Greatest Software.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
July 29, 2003Serguei Mokhov, 1 Makefile Brief Reference COMP 229, 346, 444, 5201 Revision 1.2 Date: July 18, 2004.
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), …
Creating your first C++ program
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
The Makefile (in a windows environment) Make reads its instructions from text files. An initialization file is read first, followed by the makefile. The.
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.
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)
Copyright © 2015 Curt Hill Make An Indispensable Developer’s Tool.
CMSC 1041 Introduction to C Creating your first C program.
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.
Modular Programming. Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to.
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.
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.
Problem Solving With C++ Recitation – make February 2016.
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.
Build Tools 1. Building a program for a large project is usually managed by a build tool that controls the various steps involved. These steps may include:
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
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.
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.
Compilation and Debugging
Compilation and Debugging
Makefiles Caryl Rahn.
SCMP Special Topic: Software Development Spring 2017 James Skon
Compiling from source code
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
Data Structures and Programming Techniques
CMSC 202 Additional Lecture – Makefiles
Kyle Fitzpatrick Konstantin Zak 11/29/2004
SCMP Software Development Spring 2018 James Skon
CSCE-221 Makefile Introduction
Makefiles and the make utility
Compiler vs linker The compiler translates one .c file into a .o file
Makefiles, GDB, Valgrind
SCMP Software Development Spring 2018 James Skon
SPL – PS1 Introduction to C++.
g++ features, better makefiles
The make utility (original presentation courtesy of Alark Joshi)
Presentation transcript:

Unix Makefiles COP 3330 Lecture Notes Dr. David A. Gaitros

Introduction The “make” utility in Unix is one of the original tools designed by S. I. Fieldman of AT&T Bell labs circa There are many version. What is “make”?: The tool is designed to allow programmers to efficiently compile large complex programs with many components easily. You can place the commands to compile a program in a Unix script but this will cause ALL modules to be compiled every time. The “make” utility allows us to only compile those that have changed and the modules that depend upon them.

How Does it Work? In Unix, when you type the command “make” the operating system looks for a file called either “makefile” or “Makefile”. – There are exceptions to this but we will assume that you will always have the “makefile” or “Makefile” file resident in the directory of where your program resides. This file contains a series of directives that tell the “make” utility how to compile your program and in what order. Each file will be associated with a list of other files by which it is dependent. This is called a dependency line. If any of the associated files have been recently modified, the make utility will execute a directive command just below the dependency line.

How Does it Work? The “make” utility is recursive. For instance, if a very low level utility is the only thing changed, it could cause all of the modules within a program to be re-compiled. After the utility finishes the file, it goes through and checks all of the dependencies again to make sure all are up to date.

Simple Example hello: main.o factorial.o hello.o g++ main.o factorial.o hello.o -o hello main.o: main.cpp g++ -c main.cpp factorial.o: factorial.cpp g++ -c factorial.cpp hello.o: hello.cpp g++ -c hello.cpp clean: rm -rf *o hello

The Tree hello main.o factorial.ofactorial.cpp main.cpp factorial.ohello.o hello.cpp clean

Components of a Makefile Comments Rules Dependency Lines Shell Lines Macros Inference Rules

Comments A comment is indicated by the character “#”. All text that appears after it will be ignored by the make utility until the end of line is detected. Comments can start anywhere. There are more complex comment styles that involve continuation characters but please start each new comment line with an # and avoid the more advanced features for now. Example – # – # This is a comment – projecte.exe : main.obj io.obj # this is also a comment.

Rules Rules tell make when and how to make a file. The format is as follows: – A rule must have a dependency line and may have an action or shell line after it. The action line is executed if the dependency line is out of date. – Example: hello.o: hello.cpp g++ -c hello.cpp – This shows hello.o as a module that requires hello.cpp as source code. If the last modified date of hello.cpp is newer than hello.o, than the next line (shell line) is executed. – Together, these two lines form a rule.

Dependency Lines The lines with a “:” are called dependency lines. – To the left are the dependencies – To the right are the sources needed to make the dependency. At the running of the make utility, the time and date when Project.exe was last built are compared to the dates when main.obj and io.obj were built. If either main.obj or io.obj have new dates, then the shell line after the dependency line is executed. The make process is recursive in that it will check all dependencies to make sure they are not out of date before completing the build process. It is important that all dependencies be placed in a descending order in the file. Some files may have the same dependencies. For instance, suppose that two files needed a file called bitvect.h. What would the dependency look like: main.obj this.obj: bitvect.h

Shell Lines The indented lines (must have tab) that follow each dependency line are called shell lines. Shell lines tell make how to build the target. A target can have more than one shell line. Each line must be preceded by a tab. After each shell is executed, make checks to see if it was completed without error. You can ignore this but I would not at this point.

Shell Lines After each shell line is executed, Make checks the shell line exit status. Shell lines that returning an exit status of zero (0) means without error and non-zero if there is an error. The first shell line that returns an exit status of non-zero will cause the make utility to stop and display an error. You can override this by placing a “-” in front of the shell command, but I would not do this. – Example: - gcc –o my my.o mylib.o

Macros Comes from the Greek word makros meaning large. Basically it is a shorthand or alias used in the makefile A string is associated with another usually larger string Inside the file, to expand a macro, you have to place the string inside of $( ). The whole thing is expanded during execution of the make utility.

Macros Examples of macros: HOME = /home/courses/cop4530/spring02 CPP = $(HOME)/cpp TCPP = $(HOME)/tcpp PROJ =. INCL = -I $(PROJ) –I$(CPP) –I$(TCPP) You can also define macros at the command line such as – Make DIR = /home/faculty/whalley/public_html/cop5622proj/lib2 – And this would take precedence over the one in the file.

Inference Rules Inference rules are a method of generalizing the build process. In essence, it is a sort of wild card notation. The “%” is used to indicate a wild card. Examples: %.obj : %.c $(CC) $(FLAGS) –c $(.SOURCE) All.obj files have dependencies of all %.c files of the same name.

Example of an average size makefile CC = gcc DIR = /home/faculty/whalley/public_html/cop5622proj/lib CFLAGS = -g -I$(DIR) -I. -c LFLAGS = -g opt: analysis.o flow.o io.o misc.o opt.o opts.o peephole.o regs.o vect.o $(CC) $(LFLAGS) -o opt analysis.o flow.o io.o misc.o opt.o opts.o peephole.o regs.o vect.o analysis.o: analysis.c analysis.h $(DIR)/misc.h $(DIR)/opt.h $(DIR)/vect.h $(CC) $(CFLAGS) analysis.c flow.o: $(DIR)/flow.c $(DIR)/flow.h $(DIR)/opt.h $(CC) $(CFLAGS) $(DIR)/flow.c io.o: $(DIR)/io.c $(DIR)/io.h analysis.h $(DIR)/misc.h $(DIR)/opt.h peephole.h $(DIR)/regs.h $(CC) $(CFLAGS) $(DIR)/io.c

misc.o: $(DIR)/misc.c $(DIR)/misc.h $(DIR)/opt.h $(CC) $(CFLAGS) $(DIR)/misc.c opt.o: $(DIR)/opt.c $(DIR)/opt.h $(CC) $(CFLAGS) $(DIR)/opt.c opts.o: opts.c $(DIR)/misc.h $(DIR)/regs.h $(DIR)/opt.h opts.h $(CC) $(CFLAGS) opts.c peephole.o: peephole.c $(DIR)/misc.h $(DIR)/regs.h $(DIR)/opt.h peephole.h $(CC) $(CFLAGS) peephole.c regs.o: $(DIR)/regs.c $(DIR)/regs.h $(DIR)/opt.h $(CC) $(CFLAGS) $(DIR)/regs.c vect.o: $(DIR)/vect.c $(DIR)/vect.h $(DIR)/opt.h $(CC) $(CFLAGS) $(DIR)/vect.c