epteck logo white
what are makefiles

What are Makefiles? Write Your First Makefile now!

Makefiles are important for every programmer as it makes code compilation easier. In this tutorial, I will tell you the importance of Makefiles. Besides, I will also guide you about compiling your first program with makefiles here. So lets get started! 

Why Makefiles are Necessary? 

In C or C++, you usually have a big chunk of code. So, you have to compile a lot of files and link them together to make an executable. It is doable by hand if the project is not very complex. However, it becomes difficult to manage code compilation if code becomes complex. This is where makefiles find its importance. 

Makefiles is a form of script in which you write about the appropriate files and linking rules to compile a program. Using this Makefile, you can compile your code easily with a one simple command. In short, Makefiles are special format that help us build and manage complex projects automatically. Let me demonstrate this concept with an example for more clarity. 

For example, let’s say we have a very simple calculator code with 3 functions. Each function is declared in a separate file as shown below 

  • main.c 
  • adder.c 
  • square.c 
  • Multiplier.c 
  • functions_declaration.h 

  

main.c 

#include <stdio.h> 

#include "functions.h"  

int main(){  

   printf ("the addition of 5 and 10 is %d \n",adder(5,10));  

   printf( "The square of 25 is, %d \n", square(25));  

   return 0;  
} 

 adder.c 

#include <stdio.h>  

  #include "functions.h"  

int adder(int n, int n){  

   return n+n;  
}

square.c 

#include "functions.h"    

int square(int n){  

   return n*n;
}

Multiplier.c 

#include "functions.h"  

int multiplier(int n, int m){  

   return n*m; 

}

functions.h

int adder(int n,int m);

int square(int n); 

int multiplier(int n,int m);


You can compile the above calculator code in linux terminal using the gcc command below 

gcc  main.c adder.c square.c multiplier.c -o calculator 

 

This command will compile 4 files and generate a calculator binary. You can run this binary to use your calculator. 

Its just an example project. Let’s suppose your project has hundreds or thousands file. Can you compile them using this command? Absolutely not. You will surely need a simple method to compile code. Furthermore, you can’t give such big commands to your clients. It will be very unprofessional. Therefore, in complex projects, you use Makefiles to make code compilation easier with a simple command. 

Another disadvantage of this command is that it will compile all the .c files again and again. For example, You make changes to one .c file. So, when you will run the above command to verify your changes, it will compile all the .c files again. Therefore, it will consume a lot of compilation time. Makefile can also help you solve this recompilation issue. 

The scope of Makefile is not only limited to simplifying the run command. It has many other advantages too that we will discuss in the coming sections. 

What Are Makefiles? 

By now, I hope that you know the reason of using Makefiles. Makefile is a form of script in which you write about the appropriate files and linking rules to compile any code. This scripts automates software building procedure and other complex tasks with dependencies. 

Makefiles uses make utility in unix for automating building procedures. Make utility in unix is usually used for managing tasks with multiple dependencies. This make utility in linux gets instructions from Makefile to compile your specific software. So, you define rules, flags, compilers and other such things in your Makefile for building your code. Make utility uses these instructions to build a program from you. 

Other competitors of Makefiles are Cmake, automake, ninja and Bazel. Makefiles are extensively used in embedded systems, kernel, microcontroller firmware writing and other types of software development.

Advantages of Makefiles

Before writing your first Makefile, let us see a various advantages of using Makefile now! 

  • Makefiles make code compilation easier using a simple command. You don’t have to write long gcc commands and set rules for compilation everytime you want to build a program. 
  • Makefile uses make utility in Linux. It keeps records of changes in your program. So, it recompiles only those files that have changed. In this way, it saves a lot of compilation time. Before makefiles, we have to compile every file in our code again and again. It used to cost a lot of compiling time. 
  • It automates the entire procdure. Therefore, it becomes easier to share our code with other developers and clients. They can easily compile and test changes using the Makefiles. Therefore, most of the open-source softwares uses Makefiles. 

How to Write Your First Makefile? 

In this section, I will provide you step by step guide to write your first Makefile. 

Step 1: Open a terminal by pressing ctrl+alt+t or using the menu bar. Run the below commands to make all necessary files for your first Make file project. 

mkdir sample_project 

cd sample_project 

touch hello.c 

touch main.c 

touch functions.h 

touch Makefile

Step 2:  Now, go to sample_project directory that we created in Step 1 and open hello.c file. Paste the following code there. 

#include <stdio.h>  

//defintion of hello function   

void hello() {  

   // printf() displays the string inside quotation  

   printf("Hello, World!");  

}

Step 3: Open the main.c file in the sample_project directory and paste the following code there.  

#include <stdio.h> 

#include "functions.h"  

int main(){  

   hello();  

   return 0;  

}

Step 4: open functions.h file and paste the following code there. 

// function declaration here  

void hello();

Step 5: Open Makefile and paste the following code there. 

hello: hello.c main.c  

     gcc -o hello hello.c main.c -I.

  

In line 1, the hello before ‘:’ symbol is target name. You can run make hello in the terminal to compile your code. In line 1, two .c files (hello.c and main.c) after ‘:’ sign shows the dependencies for building a target.  

In line 2, we tell the make utility how to build a target. Here, we are telling the make utility to build the code using gcc compiler. The -o flag tells about the output binary name. In our case, the output binary file name would be hello. After that, we have told compiler to compile hello.c and main.c. In the end of line 2, -I flag tells the compiler to find the header files in the present working directory.  

You can refine your header file in multiple ways. In order to improve your Makefile, you can have a look at this tutorial for more help. 

Step 6: In the terminal, run the following command in the sample_project directory. 

 make hello

Step 7: Now, run the below command. You will see a hello world printing on the terminal. 

./hello

Get in touch with us Today!