Makefiles Caryl Rahn.

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.
Makefiles  Provide a way for separate compilation.  Describe the dependencies among the project files.  The make utility.
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.
Makefiles Tutorial adapted from and myMakeTutorial.txt.
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,
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
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.
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.
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.
리눅스 : Lecture 5 UNIX 유틸리티 : text editor, compilation (make), …
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.
C Tutorial - Program Organization CS Introduction to Operating Systems.
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).
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
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.
CSI605 Introduction to make. Advantages of Make Significantly reduces the amount of time spent compiling a program. Insures that programs are compiled.
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
Makefiles CSSE 332 Operating Systems
The make utility (original presentation courtesy of Alark Joshi)
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
Large Program Management: Make; Ant
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
C Preprocessor(CPP).
Data Structures and Programming Techniques
CSE 390 Lecture 8 Large Program Management: Make; Ant
CMPSC 60: Week 4 Discussion
Large Program Management: Make; Ant
SCMP Software Development Spring 2018 James Skon
Writing Large Programs
Large Program Management: Make; Ant
CSc 352 An Introduction to make
Makefiles and the make utility
Compiler vs linker The compiler translates one .c file into a .o file
Makefiles, GDB, Valgrind
CSE 390 Lecture 8 Large Program Management: Make; Ant
SCMP Software Development Spring 2018 James Skon
SPL – PS1 Introduction to C++.
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 Caryl Rahn

Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With several files, we can compile and link our program as usual using cc addmoney.c removemoney.c main.c When compiling in this manner produces a problem, we fix the problem and recompile. But, we ended up recompiling everything with even if we had to make a very simple change to just one file. This is wasteful.

Separate compilation What we should do instead is separately compile source files to intermediate object files and then link them together So, for the files addmoney.c removemoney.c main.c We want to compile each piece separately and then link them together. When we just compile source code (without linking it together), it means that we take the .c files and generate intermediate object (.o) files.

Separate compilation To just compile source code, use the -c flag with the compiler... cc -c addmoney.c cc -c removemoney.c cc -c main.c This will generate the object files addmoney.o, removemoney.o, and main.o Finally, to link the object files (.o) into an executable that we can run, we use the compiler again (although this time it will just pass the .o files on to the linking stage): cc -o money addmoney.o removemoney.o main.o

The Unix “make” Utility Building a program consisting of multiple source modules: (addmoney.c, removemoney.c, main.c) As we have just seen, one approach is to use cc (or gcc) with the -c option to build addmoney.o, removemoney.o, and main.o cc -c removemoney.c cc -c addmoney.c cc -c main.c The files addmoney.o, removemoney.o, and main.o, are object files but they are not executable. Then cc is used to link addmoney.o, removemoney.o, main.o, and the standard C library files.

The Unix “make” Utility Building a program consisting of multiple source modules: (addmoney.c, removemoney.c, main.c) cc -o money addmoney.o removemoney.o main.o The file money is an executable object file. An alternative approach is to perform compilation and linking all in one step: cc -o money addmoney.c removemoney.c main.c The disadvantage of this method is that it is necessary to recompile all of the source files making up the program each time any one source module changes. The Unix make program is a handy utility that can be used to build things ranging from programs to documents.

make Utility Helps you to build and manage projects Types of statements that can go in a makefile macro definition – name that you define to represent a variable that may occur several times within the makefile target definition – lists the target file, its required files, and commands to execute the required files in order to produce the target. Suffix rules – indicate the relationship between target and source file suffixes (filename extensions). Suffix declarations – lists of suffixes (file extensions) used in suffix rules

make Utility target definition target: dependencies [tab] commands targets – labels that appear in column 1 and are followed by the ":" character. dependencies - a list of files following the name of the target. These are the files that are needed to make the target. The target "depends on these files." If any dependency is newer than the target, the target will be rebuilt. commands – specify the procedure for building the target. Each line must begin with the tab character, not spaces.

make Utility For a project consisting of the files main.c , removemoney.c , addmoney.c and money.h, the trivial way to compile the files and obtain an executable is cc -o money main.c removemoney.c addmoney.c A makefile for doing this would look like: money: main.o removemoney.o addmoney.o In this example, target is money. The dependencies are main.o, removemoney.o, and addmoney.o In order for make to execute correctly, it has to meet all the dependencies of money. If main.o, removemoney.o, or addmoney.o is newer than money, then make rebuilds money

make Utility - Running make on the command line There are different ways to run make. make Looks in the current directory for a file named makefile or Makefile and runs the commands for the first target make –f <filename> Looks in the current directory for a makefile with the given name and runs the commands of the first target. make <target> Looks for a file named makefile or Makefile and locates the target. This does not have to be the first target. It will run the commands for that target provided the dependencies are more recent than the target.

Make Utility Example money: main.o removemoney.o addmoney.o cc -o money main.c addmoney.c removemoney.c To build money, type either of the following commands: make Or make money

Make Utility Example money: main.o addmoney.o removemoney.o cc -o money main.c addmoney.c removemoney.c clean: rm *.o *.err In this example, there are two targets: money and clean The second target has no dependencies. The command make clean will remove all object files and all .err files.

Macros You want to use macros to make it easy to make changes. For example, if you use macros, it's easy to change the compiler and compiler options different compilers. It's easy to turn on and off debug options. Without macros, you would use a lot of search and replace. You use macros in makefiles for the same reason you define constants in programs. It's easier to update the files and make it more flexible.

Macros Predefined macro based names: $@ -- the current target’s full name $? -- a list of the target’s changed dependencies $< -- similar to $? But identifies a single file dependency and is used only in suffix rules $* -- the target file’s name without a suffix Another useful macro based facility permits one to change prefixes on the fly. The macro $(@:.o=.err) says use the target name but change the .o to .err.

Macro definition A makefile line with the following syntax MACRO-NAME = macro value. Invoked using the syntax $(MACRO-NAME) Result is that $(MACRO-NAME) is replaced by the current value of the macro. Examples OBJS = main.o removemoney.o addmoney.o CC = gcc CFLAGS = -DDBG_PIX -DDBG_HIT

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 = gcc 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 gcc -o my_prog eval.o main.o eval.o : eval.c eval.h gcc -c –g eval.c main.o : main.c eval.h gcc -c –g main.c

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 !!

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.

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 makefile The 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

Example of a makefile CC = gcc DIR = /home/faculty/crahn/public_html/cop4833/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

Class Activity Create small C program: hello.c Discuss a.out Create basic Makefile: Makefile Get Makefile working Add a clean target Modify Makefile to separate compiling source and objects.

Example of a makefile (continued) 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