MakeFile in C++ and its applications

Given a C++ program and the task is to break the entire program in the form of Makefile.
It is basically used to create .cpp file and .h file for each class/functions and its functionalities and later link them all through a Makefile command available in C++.


Example: Create a program to find the factorial and multiplication of numbers and print it.

Traditional Way





// Program to calculate factorial and
// multiplication of two numbers.
using namespace std;
// Function to find factorial
int factorial(int n)
    if (n == 1)
        return 1;
    // Recursive Function to find 
    // factorial
    return n * factorial(n - 1);
// Function to multiply two numbers
int multiply(int a, int b)
    return a * b;
// Function to print
void print()
    cout << "makefile" << endl;
// Driver code
int main()
    int a = 1;
    int b = 2;
    cout << multiply(a, b) << endl;
    int fact = 5;
    cout << factorial(5) << endl;
    return 0;



Use Makefile to run the above program:

Commands to Compile and Run above program:

Open Terminal and type commands:
g++ -c main.cpp
g++ -c print.cpp
g++ -c factorial.cpp
g++ -c multiply.cpp
g++ -o main main.o print.o factorial.o multiply.o

Note: g++ -c filename.cpp is used to create object file.

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :