Linux Tools

GNU Make

The GNU Make is a very handy utility that helps you maintain groups of programs with ease. Make will make sure that the dependencies are resolved and the program is up to date whenever you make changes to any of your files.
make takes its instructions from a spacial file called a Makefile
This is a demonstration of the GNU Make utility. For this demonstration we create a simple c++ program that prints Hello World
  • Create your source files.

hello.cpp:
#incude<iostream>
using namespace std;
extern void func();
main()
{
    cout <<"Hello ";
    func();
}

world.cpp
#include<iostream>
#include<stdlib>
using namespace std;
void func()
{
   cout <<"World"<<endl;
}


  • Create a new file and call it Makefile (The name must be so)

Inside the makefile, the structure is as follows
<TARGET>: <prerequisits>
<TAB><RULE TO MAKE TARGET>
Eg:
#We have a file called hello.cpp
#Step one is creation of an object file
hello.o: hello.cpp
g++ -c hello.cpp

#We have another file called world.cpp
#Step two is creation of an object file for this
world.o: world.cpp
g++ -c world.cpp

#Since we now have our object files
#Step three is to generate the executable helloworld
helloworld: hello.o world.o
g++ hello.o world.o -o helloworld


  • Save and quit the Makefile
  • Execute make in terminal, just type make and hit enter


Here is another simple Makefile


No comments:

Post a Comment