Make Files with C++
SCMP 318 Software Development
James Skon
Quiz - Makefiles
Open 10:00, Closes 10:15
Password: MakeIt!
What is Make?
Make Dependancies
Make Dependencies
Make Dependancies
Make Dependancies
Make
Using Make
% make
% make -f makeprog2
Using Make
target-file: prerequisite-file-list
(tab) construction-commands
An Example
Example Makefile
# Makefile for my_lab
my_lab: main1.o mylib.o openfile.o
g++ -o my_lab main1.o mylib.o openfile.o
main1.o: main1.cpp openfile.h mylib.h
g++ -c main1.cpp
mylib.o: mylib.cpp mylib.h
g++ -c mylib.cpp
openfile.o: openfile.cpp openfile.h
g++ -c openfile.cpp
clean:
rm *.o
Comment
Label and dependancies
Actions
TAB
Make
How Make Works
How Make Works
Verifying Makefile Formats
A Simple Makefile Example
A Simple Makefile Example
/* abc.cpp */
main()
{
a();
b();
c();
}�
/* a.cpp */
#include <stdio.h>
a()
{
printf("A\n");
}
/* b.cpp */
#include <stdio.h>
b()
{
printf("B\n");
}��
/* c.cpp */
#include <stdio.h>
c()
{
printf("C\n");
}
A Simple Makefile Example
# makefile : makes the ABC program
abc : a.o b.o c.o abc.o
g++ -o abc abc.o a.o b.o c.o
abc.o : abc.cpp
g++ -O -c abc.cpp
a.o : a.cpp, abc.h
g++ -O -c a.cpp
b.o : b.cpp , abc.h
g++ -O -c b.cpp
c.o : c.cpp , abc.h
g++ -O -c c.cpp
Internal Make Rules
Internal Make Rules Example
/* A sample program to illustrate the MAKE utility */
#include <stdio.h>
main()
{
printf("Working\n");
compute_sales_figures();
printf(".\n");
pay_employees();
printf(".\n");
printf("Done\n");
}
Internal Make Rules Example
pay_employees()
{
int i, k;
for (i=1; i<100; ++i)
k = i + 1;
}
Internal Make Rules Example
/* A sample program to illustrate the MAKE utility */
#include <stdio.h>
compute_sales_figures()
{
int i, k;
for (i=1; i<100; ++i)
k = i + 1;
}
Internal Make Rules Example
# makefile : makes the payroll program
# for the make tutorial
# Default construction commands will
# build the object files:
# salary.o and sales.o
payroll : salary.o sales.o
g++ salary.o sales.o -o payroll
Internal Make Rules Example
salary.o sales.o
Default construction commands
g++ -O -c file.cpp
g++ -O -c salary.cpp
g++ -O -c sales.cpp
Default construction commands
Make Macros
Make Macros
macroname = replacestring
Make Macros
Make Macros example
# payroll2.make: another makefile for the payroll program
# used in thr make tutorial.
CFLAGS = -g -O # compiler options
payroll : salary.o sales.o
g++ -o payroll salary.o sales.o
salary.o : salary.cpp
g++ -c $(CFLAGS) salary.cpp
sales.o : sales.cpp
g++ -c $(CFLAGS) sales.cpp
Make Macros example