Monday 14 June 2010

Programming in GNU/Linux environment Part 2

Intro to makefile:

In my previous blog I gave the Introduction for starting Programming in Linux environment.In this blog I'm planning for further writing about gcc compiler and options available and at last an Introduction to make file.
Whenever we compile any C program using gcc by default the output is a.out file.To specify the output we use -o option with gcc:
gcc -o main main.c
Here main will be the output file assuming main.c is the C source code file available.
Now if we have two different C/C++ program files dependent on each other for executing we can not create binary file or executable directly we have to first create object files and then combine them.For example we have main.c and reciprocal.cpp files and main function is in .c file which calls another function reciprocal in .cpp file to calculate reciprocal of a entered number.Now first we will create object files of both C and C++ programs using -c option.
gcc -c main.c
g++ -c reciprocal.cpp
These two commands will create two files named main.o and reciprocal.o respectively.Now to create object files we can use command:
g++ -o reciprocal main.o reciprocal.o
Sometimes because of optimisation by compiler or due to any other reasons our program may not work as we have expected.We use debugger to solve this problem.To use GNU debugger we use -g option with gcc compiler to add extra information for gdb(GNU debugger):
g++ -g -o reciprocal main.o reciprocal.o
One more thing we are using g++(C++) compiler to combine object files as one of the file(reciprocal.cpp) is of C++ if both files were of C we can use gcc to create binary.
Now here consider the case you have more than 2 or 3 source code files now to write gcc command again and again is time consuming and chances for error increases , here comes the makefile useful.Makefile is a collection of all gcc lines over and over again.Its very useful especially if you are making a lot of changes to a code and it has several libraries.Here change in one file means compiling all the code and running all commands again.But with makefile single make command will compile all the code in single go.
Consider the above reciprocal program.The makefile will look like:
reciprocal: main.o reciprocal.o
gcc -g -o reciprocal main.o reciprocal.o
main.o: main.c
gcc -g -c main.c
reciprocal.o: reciprocal.c
gcc -g -c reciprocal.c
Here there are two types of lines dependent and executable.All the gcc lines having a preceded tab are executables that will compile program and produce output and the lines just above tell the dependencies of output.For example reciprocal is dependent on main.o and reciprocal.o so before executing gcc -g -o reciprocal main.o reciprocal.o main.o and reciprocal.o will be produced and it continues till dependencies are satisfied.All the above content will be in file named makefile and running make command will compile the program if makefile in current directory.
If you want to delete/remove all the *.o files and reciprocal binary just created You can add clean entry in makefile using rm command.
clean:
rm -f *.o reciprocal
Hope it will help.Do comment and keep learning.