Home »
C Popular & Interesting Questions
What is Makefile for C program compilation and How to create Makefile?
Makefile in Linux for Compilation
If you have multiple source files in c, c++ and others language and want to compile them from Terminal Command, it is hard to write every time. To solve such kind of problem, we use Makefile because during the compilation of large project we need to write numbers of source files as well as linker flags are required, that are not so easy to write again and again.
What is Makefile?
Makefile is a tool to simplify or to organize code for compilation. Makefile is a set of commands (similar to terminal commands) with variable names and targets to create object file and to remove them. In a single make file we can create multiple targets to compile and to remove object, binary files. You can compile your project (program) any number of times by using Makefile.
Makefile Example
Let's understand with an example:
Suppose, we have 3 files main.c (main source file), misc.c (source file that contains function definition), misc.h (that contain function declaration). Here we will declare and define a function named myFunc() to print something – this function will be defined and declared in misc.c and misc.h respectively.
misc.c
#include <stdio.h>
#include "misc.h"
/*function definition*/
void myFunc(void)
{
printf("Body of myFunc function.\n");
}
misc.h
#ifndef MISC_H
#define MISC_H
/*function declaration.*/
void myFunc(void);
#endif
main.c
#include <stdio.h>
#include "misc.h"
int main()
{
printf("Hello, World.\n");
myFunc();
fflush(stdout);
return 0;
}
Makefile to compile these files
#make file - this is a comment section
all: #target name
gcc main.c misc.c -o main
- Save file with name "Makefile".
- Insert comment followed by # character.
- all is a target name, insert : after target name.
- gcc is compiler name, main.c, misc.c source file names, -o is linker flag and main is binary file name.
Compile program/project using Makefile
Command make is used to compile program through Makefile, since there are only one target all in the Makefile, so we do not need to mention target name because first target is got compiled automatically.
Without target name:
make
With target name:
make all
Output:
sh-4.3$ make
gcc main.c misc.c -o main
sh-4.3$ ./main
Hello, World.
Body of myFunc function.
sh-4.3$
Makefile using variables and clean target
We can also use variables in the Makefile to generalise Makefile. In this examples we are writing Makefile using variables and clean target name to remove all object (.o extension files) and binary file (main).
#make file - this is a comment section
CC=gcc #compiler
TARGET=main #target file name
all:
$(CC) main.c misc.c -o $(TARGET)
clean:
rm $(TARGET)
To compile:
make
To clean:
make clean
Output:
sh-4.3$ make
gcc main.c misc.c -o main
sh-4.3$ ./main
Hello, World.
Body of myFunc function.
sh-4.3$ make clean
rm main
sh-4.3$
Makefile with creating Object of source files and Cleaning Object files and Binary File
When we have multiple files then we can write command in Makefile to create Object files for each source file. If you do this – Only those files will be compiled which are modified.
Makefile
#make file - this is a comment section
CC=gcc #compiler
TARGET=main #target file name
all: main.o misc.o
$(CC) main.c misc.c -o $(TARGET)
clean:
rm *.o $(TARGET)
Output: When main.c and misc.c modified
sh-4.3$ make
gcc -c -o main.o main.c
gcc -c -o misc.o misc.c
gcc main.c misc.c -o main
Output: When main.c modified
sh-4.3$ make
gcc -c -o main.o main.c
gcc main.c misc.c -o main
Output: When misc.c modified
sh-4.3$ make
gcc -c -o misc.o misc.c
gcc main.c misc.c -o main
Output: When no file modified
sh-4.3$ make
gcc main.c misc.c -o main
What is rm *.o $(TARGET)?
This command will remove all object files with along with TARGET file that is main.