Download presentation
Presentation is loading. Please wait.
1
CSCE-221 Makefile Introduction
Emil Thomas 01/17/19 Based on slides by Prof. Jeremy Gibson(UMB) and Prof. Shawn Lupoli (TAMU)
2
Make Overview make is a program that automates the compilation of programs whose files are dependent on each other A program typically consists of several files, and a programmer usually only works on a few of them at a time Typically vast majority of files remain unchanged and thus do not need to be recompiled
3
Why using make and Makefile ?
automated way of compiling make will only recompile the files that need to be updated (files that depend on modified files), - faster & efficient than recompiling the whole program each time make looks at the timestamps of source files (*.cpp, *.h) that are required to generate an object file (*.o) If a source is newer then the object file, the object file needs to be recompiled Likewise if an object file is newer than the executable it needs to be re-linked can clean up mess from compiling
4
Compilation process
5
Makefile Structure A Makefile is a list of rules of what files are required to generate an object or an executable file Each rule consists of 4 parts: Target: the name of the object or executable to create Usually the final executable name and object files Dependency List: the list of files that the target is dependent upon For an object file target : the corresponding .cpp file and any user defined “#included” header files in it. (Don’t use system header files) TAB: used to set off an action Action(s): a list of actions / linux system commands to take in order to create the target (i.e. g++ …)
6
Makefile Rule foobar.o: foobar.cpp foobar.h
Dependency List The files that are required to create the object file. In this case foobar.cpp and foobar.h Target The file to create. In this case an object file: foobar.o foobar.o: foobar.cpp foobar.h g++ -ansi -Wall -c foobar.cpp <TAB> Used to signal what follows as an action Action(s) What needs to be done to create the target. In this case it is the separate compilation of foobar.cpp
7
Makefile Example & Demonstration
Consider a cpp project with 3 files : driver.cpp Airplane.cpp Airplane.h Our objective is to create a Makefile, with the end goal of a executable named “output.out” Source files setup (look at includes) Airplane.h Airplane.cpp driver.cpp #ifndef AIRPLANE_H_ #define AIRPLANE_H_ #include <string> #include <iostream> using namespace std; … #include "Airplane.h"
8
Makefile (All the project files should be in the same directory as the Makefile)
output.out: driver.o Airplane.o g++ -Wall -std=c++11 driver.o Airplane.o -o output.out driver.o: driver.cpp Airplane.h g++ -std=c++11 -Wall -c driver.cpp Airplane.o: Airplane.cpp Airplane.h g++ -std=c++11 -Wall -c Airplane.cpp # -f removes any error messages if the file is not present # *.o mean all files ending with .o clean: rm -rf *.o rm -f *.out
9
Demonstration in compute.cs.tamu.edu
Tabs, not spaces compiling, running and cleaning the project using Makefile Selective compiling Upto date target Changing timestamp of .cpp .h files (recompiling efficiently using Makefile) Adding a run target
10
Makefile another Example
Consider a cpp project with files: main.cpp Point.h Point.cpp Rectangle.h Rectangle.cpp Our objective is to create a makefile, with the end goal of a executable named “proj1” A file/target may depend on one or more other files Need to ensure correct compilation order
11
Dependency Graph of the files
Point.h proj1 Point.cpp Point.o Rectangle.o Rectangle.h Rectangle.cpp main.cpp main.o Link Include Compile Depends on Source:
12
Makefile proj1: main.o Point.o Rectangle.o
g++ -Wall –std=c++11 -o proj1 main.o Point.o Rectangle.o main.o: main.cpp Rectangle.h Point.h g++ -Wall -std=c++11 -c main.cpp Point.o: Point.cpp Point.h g++ -Wall -std=c++11 -c Point.cpp Rectangle.o: Rectangle.cpp Rectangle.h Point.h g++ -Wall -std=c++11 -c Rectangle.cpp clean: rm *.o rm proj1 run: ./proj1
13
Exercise to be submitted to ecampus
Login to compute.cs.tamu.edu using your netid and password Putty in Windows Terminal in mac or Linux Create a folder csce221 ; and a subfolder lab1_makefile1 and go inside it using the cd command mkdir csce221 cd csce221 mkdir lab1_makefile1 cd lab1_makefile1 Copy and extract the tar file using cp /tmp/lecturer.tar . tar xvf lecturer.tar Code the cpp and .h files and complete the included Makefile using your favorite editor See the comments inside the cpp and .h files Compile and run the code, using the Makefile By executing “make” “make run” %You should have a target named ”run” inside your Makefile “make clean” Compress the .cpp , .h and Makefile to a single zip file(Don’t include .o or the executable or the original tar file) Upload the zip file to ecampus under Makefile1
14
Questions ?
15
https://goo.gl/forms/8jQ0296hACw4Lftl2
Complete the survery
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.