Makefiles  Provide a way for separate compilation.  Describe the dependencies among the project files.  The make utility.

Slides:



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

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.
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.
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.
Basic linux shell commands and Makefiles. Log on to engsoft.rutgers.edu Open SSH Secure Shell – Quick Connect Hostname: engsoft.rutgers.edu Username/password:
Introduction to Make Updated by Prasad Spring 2000.
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.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
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.
Introduction Use of makefiles to manage the build process Declarative, imperative and relational rules Environment variables, phony targets, automatic.
Jump to first page (C) 1998, Arun Lakhotia 1 Software Configuration Management: Build Control Arun Lakhotia University of Southwestern Louisiana Po Box.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)
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.
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.
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)
Copyright © 2015 Curt Hill Make An Indispensable Developer’s Tool.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
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.
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.
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.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
CSI605 Introduction to make. Advantages of Make Significantly reduces the amount of time spent compiling a program. Insures that programs are compiled.
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"
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
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.
Compilation and Debugging
Compilation and Debugging
Large Program Management: Make; Ant
Makefiles Caryl Rahn.
SCMP Special Topic: Software Development Spring 2017 James Skon
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Makefiles and the make utility
C Preprocessor(CPP).
Data Structures and Programming Techniques
CMPSC 60: Week 4 Discussion
CMSC 202 Additional Lecture – Makefiles
SCMP Software Development Spring 2018 James Skon
Writing Large Programs
GNU Make.
Makefiles and the make utility
Compiler vs linker The compiler translates one .c file into a .o file
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:

Makefiles  Provide a way for separate compilation.  Describe the dependencies among the project files.  The make utility.

Using makefiles Naming:  makefile or Makefile are standard  other name can be also used Running make make make –f filename – if the name of your file is not “makefile” or “Makefile” make target_name – if you want to make a target that is not the first one

makefiles content Makefiles content  rules : implicit, explicit  variables (macros)  directives (conditionals)  # sign – comments everything till the end of the line  \ sign - to separate one command line on two rows

Sample makefile  Makefiles main element is called a rule: Example: my_prog : eval.o main.o g++ -o my_prog eval.o main.o eval.o : eval.c eval.h g++ -c eval.c main.o : main.c eval.h g++ -c main.c _________________________ # -o to specify executable file name # -c to compile only (no linking) target : dependencies TAB commands#shell commands

Variables The old way (no variables) A new way (using variables) Defining variables on the command line: Take precedence over variables defined in the makefile. make C=cc C = g++ OBJS = eval.o main.o HDRS = eval.h my_prog : eval.o main.o $(C) -o my_prog $(OBJS) eval.o : eval.c $(C) –c –g eval.c main.o : main.c $(C) –c –g main.c $(OBJS) : $(HDRS) my_prog : eval.o main.o g++ -o my_prog eval.o main.o eval.o : eval.c eval.h g++ -c –g eval.c main.o : main.c eval.h g++ -c –g main.c

Implicit rules  Implicit rules are standard ways for making one type of file from another type.  There are numerous rules for making an.o file – from a.c file, a.p file, etc. make applies the first rule it meets.  If you have not defined a rule for a given object file, make will apply an implicit rule for it. Example: Our makefileThe way make understands it my_prog : eval.o main.o $(C) -o my_prog $(OBJS) $(OBJS) : $(HEADERS) my_prog : eval.o main.o $(C) -o my_prog $(OBJS) $(OBJS) : $(HEADERS) eval.o : eval.c $(C) -c eval.c main.o : main.c $(C) -c main.c

Defining implicit rules %.o : %.c $(C) -c –g $< C = g++ OBJS = eval.o main.o HDRS = eval.h my_prog : eval.o main.o $(C) -o my_prog $(OBJS) $(OBJS) : $(HDRS) Avoiding implicit rules - empty commands target: ; # Implicit rules will not apply for this target.

Automatic variables Automatic variables are used to refer to specific part of rule components. eval.o : eval.c eval.h g++ -c eval.c - The name of the target of the rule ( eval.o ). $< - The name of the first dependency ( eval.c ). $^ - The names of all the dependencies ( eval.c eval.h ). $? - The names of all dependencies that are newer than the target target : dependencies TAB commands#shell commands

make options make options: -f filename - when the makefile name is not standard -t - (touch) mark the targets as up to date -q - (question) are the targets up to date, exits with 0 if true -n - print the commands to execute but do not execute them / -t, -q, and -n, cannot be used together / -s - silent mode -k - keep going – compile all the prerequisites even if not able to link them !!

Phony targets Phony targets: Targets that have no dependencies. Used only as names for commands that you want to execute. clean : rm $(OBJS) __________________ To invoke it: make clean Typical phony targets: all – make all the top level targets.PHONY : all all: my_prog1 my_prog2 clean – delete all files that are normally created by make print – print listing of the source files that have changed.PHONY : clean clean: rm $(OBJS) or

VPATH VPATH  VPATH variable – defines directories to be searched if a file is not found in the current directory. VPATH = dir : dir … / VPATH = src:../headers /  vpath directive (lower case!) – more selective directory search: vpath pattern directory / vpath %.h headers /  GPATH: GPATH – if you want targets to be stored in the same directory as their dependencies.

Variable modifiers C = g++ OBJS = eval.o main.o SRCS = $(OBJS,.o=.c) #!!! my_prog : $(OBJS) $(C) -g -c $^ %.o : %.c $(C) -g -c S< $(SRCS) : eval.h

Conditionals (directives) Possible conditionals are: if ifeq ifneq ifdef ifndef All of them should be closed with endif. Complex conditionals may use elif and else. Example: libs_for_gcc = -lgnu normal_libs = ifeq ($(CC),gcc) libs=$(libs_for_gcc) #no tabs at the beginning else libs=$(normal_libs) #no tabs at the beginning endif