English 中文(简体)
Makefile - Why Makefile ?
  • 时间:2024-09-17

Why Makefile?


Previous Page Next Page  

Compipng the source code files can be tiring, especially when you have to include several source files and type the compipng command every time you need to compile. Makefiles are the solution to simppfy this task.

Makefiles are special format files that help build and manage the projects automatically.

For example, let’s assume we have the following source files.

    main.cpp

    hello.cpp

    factorial.cpp

    functions.h

main.cpp

The following is the code for main.cpp source file −

#include <iostream>

using namespace std;

#include "functions.h"

int main(){
   print_hello();
   cout << endl;
   cout << "The factorial of 5 is " << factorial(5) << endl;
   return 0;
}

hello.cpp

The code given below is for hello.cpp source file −

#include <iostream>

using namespace std;

#include "functions.h"

void print_hello(){
   cout << "Hello World!";
}

factorial.cpp

The code for factorial.cpp is given below −

#include "functions.h"

int factorial(int n){
   
   if(n!=1){
      return(n * factorial(n-1));
   } else return 1;
}

functions.h

The following is the code for fnctions.h −

void print_hello();
int factorial(int n);

The trivial way to compile the files and obtain an executable, is by running the command −

gcc  main.cpp hello.cpp factorial.cpp -o hello

This command generates hello binary. In this example we have only four files and we know the sequence of the function calls. Hence, it is feasible to type the above command and prepare a final binary.

However, for a large project where we have thousands of source code files, it becomes difficult to maintain the binary builds.

The make command allows you to manage large programs or groups of programs. As you begin to write large programs, you notice that re-compipng large programs takes longer time than re-compipng short programs. Moreover, you notice that you usually only work on a small section of the program ( such as a single function ), and much of the remaining program is unchanged.

In the subsequent section, we see how to prepare a makefile for our project.

Advertisements