Tutorials Point


  Makefile Tutorial
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     About TP     Advertising  

Why do we need a Makefile

previous next AddThis Social Bookmark Button


Advertisements

For this session assume you have following source files.

  • main.cpp
  • hello.cpp
  • factorial.cpp
  • functions.h

Content of main.cpp
#include <iostream.h>

#include "functions.h"

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

Content of hello.cpp
#include <iostream.h>

#include "functions.h"

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

Content of factorial.cpp
#include "functions.h"

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

Conetnt of functions.h
void print_hello();
int factorial(int n);

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

CC  main.cpp hello.cpp factorial.cpp -o hello

This above command will generate hello binary. In our example we have only four files and we know the sequence of the function calls so it may be feasible to write the above command by hand and prepare a final binary. But for the large project where we will have thausands 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 larger programs, you will notice that re-compiling larger programs takes much longer than re-compiling short programs. Moreover, you notice that you usually only work on a small section of the program (such as a single function that you are debugging), and much of the rest of the program remains unchanged.

In subsequent sections we will see how to prepare a makefile for our project.



previous next Printer Friendly

Advertisements


  

Advertisements