20130425

How to use make file in CPP C+++ C

/*What is makefile  ?
---------------------------------------
makefile is a script/utility  that contains target, dependencies and actions to compile modified file/files in a project of multple files.  */

/*SYNTAX
-----------------------------------
target:dependencies
[tab]  actions
.
.

 */

/*HOW To USE MAKEFILE  in Cpp
---------------------------------------
steps to execut:

make -f newmakefile
newmakefile //generating .o
newmakefile //generating executable target_bin 
target_bin //execute target_bin
*/

//makefile
//-----------------------------------------------
all:main.o module.o
    gcc main.o module.o -o target_bin
main.o:main.c module.h
    gcc -I . -c main.c
module.o:module.c module.h
    gcc -I . -c module.c


//module.c contailns definition of the function1
//---------------------------------------------------
#include "module.h"

void function1()
{
    printf("\nHeloo function from Makefile\n");
}


//module.h contains the headers and declaraion of function1
//---------------------------------------------------
#include<stdio.h>
void function1();

//main.c contains the call to the function1
//---------------------------------------------------
//stdio.h not needed as included in module.h
#include "module.h"
int main()
{
    function1();
    return 0;
}

//I . specifies to look header files in current direcory
//all:  is a special target that take object files as depenedencies
//main.o: is filename target whose dependency is src file
//clean: is a special target that takes no dependency.
//        rm -rf   *.o
//        rm target_bin
//makefile first compiles the files with no dependency then
//file which has dependency of compiled files and so on
Previous                             Home                               Next

No comments:

Post a Comment