Professional Documents
Culture Documents
Operating System Lab (Week 1) : Part 1: Makefile
Operating System Lab (Week 1) : Part 1: Makefile
Part 1: Makefile
August 2020
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 1 / 14
Makefile
Makefile
Compiling the sourcecode files can be tiring
Especially when you have to include several source files and type the
compiling commands every time
Makefiles are the solution
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 2 / 14
Makefile
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 3 / 14
Makefile
Makefile
Make allows us to automate the process of building target file(s) from
source file(s)
At the basic level, it can be used to perform simple compilation of
multiple program files
At the advance level, it can allow us to cascade changes in a single
file to multiple dependant files and send the details to the concerned
users as an email
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 4 / 14
Makefile
Makefile
Uses timestamps to avoid processing unneccessary files
If the timestamp of target is newer than all the sources, do nothing
Else, one of the source files have been updated after the target,
therefore the target will be rebuilt
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 5 / 14
Makefile
Makefile
A file that can be only named makefile, Makefile or GNUmakefile
It is the “program” that make uses to build the target
Contains rules indicating the targets that are to be built using which
source files using which compilers along with related arguments passed
Target can be a file or an action to be taken, in the latter case it is
called a “phony target”
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 6 / 14
Rules are of the form
The “command” defines what has to be actually done to the source files
to produce the target. It can also contain shell keywords like touch, echo,
rm etc.
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 7 / 14
Example
main.cpp is:
#include <iostream.h>
#include "functions.h"
int main()
{print_hello();
cout << endl;
cout << "The factorial of 5 is " << factorial(5) << endl;
return 0;
}
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 8 / 14
Example Cont’d
hello.cpp is:
#include <iostream.h>
#include "functions.h"
void print_hello()
{cout << "Hello World!";}
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 9 / 14
Example Cont’d
factorial.cpp is:
#include "functions.h"
int factorial(int n)
{
if(n!=1){return(n * factorial(n-1));}
else return 1;
}
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 10 / 14
Example Cont’d
functions.h is:
void print_hello();
int factorial(int n);
The trivial way to compile the files and obtain an executable is by running
the following command:
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 11 / 14
Makefile
The makeprogram allows you to use macros, which are similar tovariables.
Macros are defined in a Makefile as = pairs. An examplehas been shown
below:
MACROS= -me
PSROFF= groff -Tps
DITROFF= groff -Tdvi
CFLAGS= -O -systype bsd43
LIBS = "-lncurses -lm -lsdl"
MYFACE = ":*)"
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 12 / 14
Makefile
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 13 / 14
Sample Makefile
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 1) August 2020 14 / 14