Coders Packet

Simple Makefile using C++

By Vivek Kumar Singh

In this project, I'll create a simple makefile for two CPP files. We'll also learn how do we create a makefile and connect it to different dependencies.

Before we learn about makefile and how to use it. We should learn about Makefile.

What is makefile?

Makefile is a simple text file, which is used to organize different CPP files, header files, and other dependent files. Here dependent files denote other functions files and others. Makefile is widely used in the Software development field. It contains a set of commands which automate the most work. 

Imagine we want to develop Software that contains lots of files, and after each small modification in some part of the file, we have to recompile whole a chunk of files. Which will take lots of time. Here, Makefile becomes useful because it only compiles those files which have been modified recently.. It does not recompile other files which have been not modified. So it Saves lots of time. The makefile has other uses too, but here we'll talk about his.

Note: I've used the Window subsystem of Linux and vim, ubuntu to create a Makefile. I've some commands to run in the terminal. If you're using windows then it'll be different for you. You will need to ".exe" file.

Now we'll see all the files and their dependency files.

Makefile

# the compiler: GCC for C program, define as g++ for C++

CC=g++

# compiler flags:
#  -g     - this flag adds debugging information to the executable file
#  -Wall  - this flag is used to turn on most compiler warnings

CFLAGS=-g -Wall

# The build target 
#TARGET = myprogram
#all: $(TARGET)
#output is a target file which will be created after all execution

all: output  

#output file is dependent on three different object files 
//main.o hello.o Fibonacci.o

output: main.o hello.o Fibonacci.o
  $(CC) main.o hello.o Fibonacci.o -o output

#main.o file is dependent on main.cpp file

main.o: main.cpp
  $(CC) $(CFLAGS) main.cpp

#hello.o file is dependent on hello.cpp file

hello.o: hello.cpp
  $(CC) $(CFLAGS) hello.cpp

#Fibonacci.o file is dependent on Fibonacci.cpp

Fibonacci.o: Fibonacci.cpp
  $(CC) $(CFLAGS) Fibonacci.cpp

#Clean command for cleaning object file
 
clean:
  rm -rf *.o output

 

Main.cpp : Main Driver program

#include
using namespace std;
#include"functions.h"
int main(){
  cout<<"Printing data from 1st File\n";
  print_hello();
  int n;
  cout<<"------------Enter the Nth Number-------------: ";
  cin>>n;
  cout<<"Printing data from 2nd File\n";
  cout<<"The "<<n<<"th Fibonacci Number is: "<<fibo(n)<<endl;
  return 0;
}

 

hello.cpp : CPP implementation of print_hello function

#include
using namespace std;
#include"functions.h"
void print_hello(){
  cout<<"-----------------Hello World-----------------:\n";
}

 

Fibonacci.cpp : CPP implementation of fibo function

#include"functions.h"
int fibo(int n){
  if(n<=1){
    return n;
  }
  return fibo(n-1)+fibo(n-2);
}

Functions.h : Header file for different function

void print_hello();
int fibo(int n);

 

We need to simply run the make command in the Ubuntu terminal that will create all the object files for available CPP files. After that, we need to run the  ./output to see the result. To clean all the object files we have to run make clean.

Download project

Reviews Report

Submitted by Vivek Kumar Singh (vikku19)

Download packets of source code on Coders Packet