Saturday 16 September 2023

Makefile

 If we are compiling a lot of source code files and something goes wrong half way through, it might be nice to be able to pick where we left off in order to finish compiling after we fix the error. Below is an example of a simple Makefile


make command will follow the Makefile and  some of the make command directives are below:-


make clean
make install           
make all
make uninstall

====================

root@debian:~# cat Makefile

all: program

program: main.o  factorial.o

     g++ main.o  factorial.o -o program

main.o: main.cpp

     g++ -c main.cpp

factorial.o: factorial.cpp

     g++ -c factorial.cpp

clean:

     rm -rf *.o program

=====================


=====================

root@debian:~# cat factorial.cpp

#include "functions.h"

int factorial(int n){

   if(n!=1){

      return(n * factorial(n-1));

   } else return 1;

}

======================


======================

root@debian:~# cat functions.h

int factorial(int n);

======================


======================

root@debian:~# cat main.cpp

#include <iostream>

using namespace std;

#include "functions.h"

int main(){

   cout << endl;

   cout << "The factorial of 5 is " << factorial(5) << endl;

   return 0;

}

=======================

No comments:

Post a Comment