Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.

Slides:



Advertisements
Similar presentations
The make Utility Programming Tools and Environments Winter 2006.
Advertisements

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.
The Makefile utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
David Notkin Autumn 2009 CSE303 Lecture 20 static make.
1 CSE 390 Lecture 8 Large Program Management: Make; Ant slides created by Marty Stepp, modified by Josh Goodwin
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.
Software Engineering Recitation 7 Suhit Gupta. Review Anything from last time???
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
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.
Introduction to Make Updated by Prasad Spring 2000.
Guide To UNIX Using Linux Third Edition
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.
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.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
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).
1 CSE 390 Lecture 8 Large Program Management: Make; Ant slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson
The Make utility. Motivation Small programs all in single cpp file “Not so small” programs : Many lines of code Multiple components More than one programmer.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Program Development Tools GNU make (much of this material is adapted from “GNU Make” by Richard Stallman) The make utility automatically determines which.
Data Display Debugger (DDD)
Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope.
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.
Make: File Dependency System Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
Multiple File Compilation and linking By Bhumik Sapara.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
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
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.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Multiple file project management & Makefile
Large Program Management: Make; Ant
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.
Compilation and Debugging
Compilation and Debugging
Makefiles Caryl Rahn.
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
Large Program Management: Make; Ant
Makefiles and the make utility
Data Structures and Programming Techniques
CSE 390 Lecture 8 Large Program Management: Make; Ant
CMPSC 60: Week 4 Discussion
Large Program Management: Make; Ant
Build Tools (make) CSE 333 Autumn 2018
Large Program Management: Make; Ant
GNU Make.
Makefiles and the make utility
Makefiles, GDB, Valgrind
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.
Large Program Management: Make; Ant
Large Program Management: Make, Ant
Presentation transcript:

Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey

Why Makefiles? Unix has been around for a long, long time Makefiles have been around about as long (1977) Makefiles are the defacto standard when it comes to Unix compilation Many modern projects just let the IDE do dependency tracking IDEs are great for that, but have trouble with general non-language build instructions 2

More on make Various versions have floated around over the years Most Unix systems have GNUmake, which we’ll use, or BSDmake It has some non-portable (to other make) things, but that’s usually not a problem Most open-source projects use make, usually generated by the auto-tools 3

What do makefiles do? A makefile is a set of instructions basically telling the ‘make’ program how to do something It’s that general--it doesn’t have to be compiling source, but it mostly is It’s a program that looks at dependencies and ‘does something’ when those dependencies have been modified 4

Make for compilation The most common use of make is for compilation Doing anything system admin related will often force you to do this more than you can imagine Packages, projects, etc, especially open-source projects, are often compiled from scratch Kernels are also recompiled, so understanding make is good for that too (even though today it’s mostly automated) 5

Make syntax Easy stuff first: # begins a comment, regardless of where it’s placed It doesn’t line-wrap It can begin in the middle of a line

Rules and Targets Rules tell make how to execute a series of commands target should be the result of the make dependencies are what make checks to determine if target should be remade target: dependencies... commands...

Minor Issues Makefiles are particular about spacing Your set of commands each occur on one line You MUST put a tab character before each command The commands can be any Unix program found in your path

More on Targets Make works by looking at the modification date of the target and dependencies If the target doesn’t exist, it executes the associated commands If the target date is older than any of the dependencies, the commands are executed

An Aside on C As a Unix user/admin, you may have to compile projects to add functionality For example, you may want to add a feature for your specific hardware to the kernel Many of these are written in C, so it’s useful to understand what C is and looks like

C Compilation C supports separate compilation, meaning that any C file can be turned into code that can be combined Each C file is turned into an object file by the compiler: gcc -c file.c Here, gcc is the compiler, the flag -c says to turn it into an object file, by default, it will be called file.o

C Compilation... Once you have a collection of object files, they are combined into an executable file gcc -o myprog file.o foo.o bar.o This results in a program called myprog that can be executed Every C program links with the C library Every Unix system is based on the C library, so it’s always there

An Example # example taken from the GNU make manual edit : main.o kbd.o command.o display.o \ insert.o cc -o edit main.o kbd.o command.o display.o \ insert.o main.o : main.c defs.h cc -c main.c kbd.o : kbd.c defs.h command.h cc -c kbd.c command.o : command.c defs.h command.h cc -c command.c display.o : display.c defs.h buffer.h cc -c display.c insert.o : insert.c defs.h buffer.h cc -c insert.c clean : rm edit main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o

Things to Note Your target, dependencies and commands can only run a single line--no newlines You continue a line with a \, which escapes the newline Dependencies can refer to targets in the Makefile also We have a special target called ‘clean’, which removes all the old object files

Variables make can use variables to simplify the Makefile We can use them to make sure we don’t miss things, like adding all the dependencies objects = main.o kbd.o \ command.o display.o \ insert.o edit : $(objects) cc -o edit $(objects)

Understanding the ‘clean’ rule By default, make runs down the list of targets and executes them if they’re not up to date The ‘clean’ rule doesn’t have dependencies and never exists If everything is up to date, clean can be run automatically (not what you want!) Instead, insert a.PHONY target ahead of it!

The clean rule in action clean can now only be run if you type ‘make clean’ The preceding - prevents an echo to the screen when it’s executing.PHONY : clean : -rm edit $(objects)

Name of your Makefile GNU make tries the following: GNUmakefile, makefile, Makefile Usually, you should use the last two Most source files are lower-case, therefore Makefile appears at the top of a sorted list of files If you pass -f, you can specify the makefile name