Intermediate C Programming (2nd)
Yung-Hsiang Lu, Purdue University
Geroge K. Thiruvathukal, Loyola University Chicago
CRC Press
ISBN 9781032189819
Chapter 05 Test Programs
Makefile
C Programs has three formats
Compilation + Linking
Text Format This book uses gcc Executable Format
Human Readable Machine Readable
Compilation Linking
Object Format
gcc -c Machine Readable gcc
These formats allow the same programs (text format) to run
on different types of machines.
C Programs has three formats
gcc -c
Text File (t1.c) Object File (t1.o)
gcc -c
Text File (t2.c) Object File (t2.o)
gcc -c Executable File
Text File (t3.c) Object File (t3.o)
gcc -c
Text File (t4.c) Object File (t4.o) Linking
Compilation
C Programs has three formats
Text File (t1.c) Object File (t1.o)
Text File (t2.c) Object File (t2.o)
Executable File
Text File (t3.c) Object File (t3.o)
gcc -c
Text File (t4.c) Object File (t4.o) Linking
Compilation
Two-Stage process to create executable
• gcc should always have the warnings turned on
• keep track of which .c files have been changed
• compile all changed .c files to generate .o files
• link .o files to create executable
Two-Stage process to create executable
• gcc should always have the warning turned on
• keep track of which .c files have been changed
• compile all changed .c files to generate .o files
• link .o files to create executable
This is a lot of work.
Fortunately, you can use Makefile.
comments start with #
Makefile (top)
Makefile (top)
define symbols
Makefile (top)
use previously defined symbols
Makefile (top)
define symbols for conditional compilation
Makefile (top)
Makefile (Scroll Down)
OBJS = add.o div.o mul.o
main.o solution.o sub.o
Makefile (Scroll Down)
target: dependence
(TAB) action
Makefile (Scroll Down)
compile .c to .o
Makefile 2
Makefile
• Define symbols for gcc warnings and select code sections
• Keep track of which .c files have been changed
• compile all changed .c files to generate .o files
• link .o files to create executable
target: dependence
replace .c in SRCS by .o to create OBJS
add.o div.o mul.o main.o solution.o sub.o
create .o file by gcc –c .c file
remove all object files
add warnings
conditional compilation
TESTFLAGS is not used
Hierarchical Makefile
ls –R: show subdirectories
Some subdirectories
All subdirectories
run make in a subdirectory
All subdirectories
Three subdirectories
ignore one directory