Creating A Makefile For An Application

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Ex No.

CREATING A MAKEFILE FOR AN APPLICATION

-01-2016

AIM:
To create a makefile for an application.

SOFTWARE TOOLS USED:


1. GCC Toolchain
2. GNU Make utility

INTRODUCTION TO GNU MAKE UTILITY:


GNU Make is a tool which controls the generation of executables and other non-source files of a
program from the program's source files.
Make gets its knowledge of how to build your program from a file called the makefile, which lists each
of the non-source files and how to compute it from other files. When you write a program, you should
write a makefile for it, so that it is possible to use Make to build and install the program.

CAPABILITIES OF MAKE:

Make enables the end user to build and install your package without knowing the details of how
that is done -- because these details are recorded in the makefile that you supply.
Make figures out automatically which files it needs to update, based on which source files have
changed. It also automatically determines the proper order for updating files, in case one nonsource file depends on another non-source file.
As a result, if you change a few source files and then run Make, it does not need to recompile
all of your program. It updates only those non-source files that depend directly or indirectly on
the source files that you changed.

Make is not limited to any particular language. For each non-source file in the program, the
makefile specifies the shell commands to compute it. These shell commands can run a compiler
to produce an object file, the linker to produce an executable, ar to update a library, or TeX or
Makeinfo to format documentation.
Make is not limited to building a package. You can also use Make to control installing or
deinstalling a package, generate tags tables for it, or anything else you want to do often enough
to make it worth while writing down how to do it.

MAKE RULES AND TARGETS


target: dependencies ...
commands
...

15EE52 Real-Time Systems Laboratory

Page 1

When you run Make, you can specify particular targets to update; otherwise, Make updates the first
target listed in the makefile. Of course, any other target files needed as input for generating these
targets must be updated first.
Make uses the makefile to figure out which target files ought to be brought up to date, and then
determines which of them actually need to be updated. If a target file is newer than all of its
dependencies, then it is already up to date, and it does not need to be regenerated. The other
target files do need to be updated, but in the right order: each target file must be regenerated
before it is used in regenerating other targets.
Source: https://www.gnu.org/software/make/
EXPERIMENT:
1. Create a C application project involving several C source files and associated header files. In
our application we created 10 C source files and a header file. The application is a simple
calculator application.
2. The contents of main source file, main.c .
/* main.c */
#include<stdio.h>
#include"calculator.h"
int main()
{
int a,b,c,ch;
a=get_number(1);
b=get_number(2);
print_menu();
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: c=add(a,b);
print_result(c);
break;
case 2: c=sub(a,b);
print_result(c);
break;
case 3: c=mul(a,b);
print_result(c);
break;
case 4: c=div(a,b);
print_result(c);
break;
case 5: c=mod(a,b);
print_result(c);
break;
default:print_error(ch);
}
return 0;
}

15EE52 Real-time Systems Laboratory

Page 2

3. Contents of other 10 files getnumber.c , print_menu.c ,add.c ,sub.c ,mul.c ,div.c ,mod.c
,print_result.c ,print_error.c
get_number.c
int get_number(int x)
{
int y;
printf("Enter number %d : ",x);
scanf("%d",&y);
return y;
}

print_menu.c
void print_menu()
{
printf("\nMENU");
printf("\n1.ADD");
printf("\n2.SUBTRACT");
printf("\n3.MULTIPLY");
printf("\n4.DIVIDE");
printf("\n5.MODULO");
}

add.c
int add(int x,int y)
{
int t;
t=x+y;
return t;
}

mul.c
int mul(int x,int y)
{
int t;
t=x*y;
return t;
}

sub.c
int sub(int x,int y)
{
int t;
t=x-y;
return t;
}

div.c
int div(int x,int y)
{
int t;
t=x/y;
return t;
}

mod.c
int mod(int x,int y)
{
int t;
t=x%y;
return t;
}

15EE52 Real-time Systems Laboratory

Page 3

print_result.c
void print_result(int x)
{
printf("Result is %d",x);
}

print_error.c
void print_error(int x)
{
printf("%d is invalid choice !!!",x);
}

calculator.h
void print_menu();
int get_number(int);
int add(int,int);
int sub(int,int);
int mul(int,int);
int div(int,int);
int mod(int,int);
void print_result(int);
void print_error(int);

DEPENDENCY TREE
Calculator.exe
main.o

main .c

get_number.o

getnumber.c

print_menu.o

print_menu.c

add.o

add.c

sub.o

sub.c

mul.o

mul.c

div.o

div.c

mod.o

mod.c

print_result.o

print_result.c

print_error.o

print_error.c

15EE52 Real-time Systems Laboratory

Page 4

4. Create a makefile as shown below using the dependency tree. The name of the file can be any
of the following three names GNUmakefile ,Makefile or makefile.
Makefile
Calculator.exe : main.o get_number.o print_menu.o add.o sub.o mul.o
div.o mod.o print_result.o print_error.o
gcc -o Calculator.exe *.o
main.o: main.c
gcc -c main.c
get_number.o: get_number.c
gcc -c get_number.c
print_menu.o: print_menu.c
gcc -c print_menu.c
add.o:add.c
gcc -c add.c
sub.o:sub.c
gcc -c sub.c
mul.o:mul.c
gcc -c mul.c
div.o:div.c
gcc -c div.c
mod.o:mod.c
gcc -c mod.c
print_result.o:print_result.c
gcc -c print_result.c

print_error.o:print_error.c
gcc -c print_error.c
clean:
rm *.o Calculator.exe

The source files, header file and makefile related to Calculator application are placed in a folder.

15EE52 Real-time Systems Laboratory

Page 5

Fig 1.1 : Calc folder containing C source files ,header files and Makefile

Fig 1.2 : Invocation of Make utility using make command

15EE52 Real-time Systems Laboratory

Page 6

Fig 1.3 : Running the executable file

Fig 1.4 : Source file print_result.c is modified and hence newer than Calculator.exe

15EE52 Real-time Systems Laboratory

Page 7

Fig 1.5 : Only print_result.c is compiled and linked with Calculator.exe,The modified output is
also shown

Fig 1.6 : Invocation of make clean to remove executable files and object files from project
folder
RESULT: Thus Makefile was created and used with GCC ToolChain and GNU Make utility.

15EE52 Real-time Systems Laboratory

Page 8

You might also like