Friday, January 28, 2011

~GCC Basics~

Hello Everyone,

In this article we would discuss on yet another wonderful software GCC(GNU
compiler collection). GCC is a portable compiler—it runs on most platforms
available today, and can produce output for many types of processors. In
addition to the processors used in personal computers, it also supports
microcontrollers, DSPs and 64-bit CPUs. "GCC is not only a native compiler—it can also cross-compile any program, "producing executable files for a different system from the one used by GCC "itself. This allows software to be compiled for embedded systems which are not "capable of running a compiler." GCC is written in C with a strong focus on portability, and can compile itself, so it can be adapted to new systems easily.


Compilation refers to the process of converting a program from the textual
source code, in a programming language such as C or C++, into machine code,
the sequence of 1’s and 0’s used to control the central processing unit (CPU) of the computer. This machine code is then stored in a file known as an executable file, sometimes referred to as a binary file.

Lets understand the steps and meaning of different kind of flags which can be passed to. Here is the basic C program (hello.c)


#include"stdio.h"
#include"stdlib.h"

int main(int argc, char *argv[], char *env[]) {

printf ("Hello, world!\n");
return 0;
}


==>
The sequence of commands executed by a single invocation of GCC consists of
the following stages:
• preprocessing (to expand macros)
• compilation (from source code to assembly language)
• assembly (from assembly language to machine code)
• linking (to create the final executable

// The preprocessor step
mantosh@ubuntu:~$cpp hello.c > hello.i

// The compiler step
mantosh@ubuntu:~$gcc -Wall -S hello.i

// The assembler step
mantosh@ubuntu:~$as hello.s -o hello.o

// The linker step
mantosh@ubuntu:~$gcc hello.o


Their is mainly 4 steps which gcc does internally during compilation.After the linker step gcc would generate the final executable file name a.out in the current directory.If we run this a.out we should get output like this:

mantosh@ubuntu:~$./a.out
Hello, world!

However gcc also provided the option "-save-temps" for the user.So if user does not want to run those 4 steps individually, he can give this option and gcc would save all the temporary file generated during whole compilation process. There is one more important flag "-g" which is necessary if we want to use gdb or other debugger for debugging the program. So most of time if we give command like:

mantosh@ubuntu:~$ gcc -save-temps -g hello.c -o hello

gcc would save all temporary files like(hello.i, hello.s,hello.o) in the current working directory. -g flag would add useful symbols in the executable file hello so that later on it can be debugged. There are thousands of different flags/option which are available with gcc. Interested reader can check the official GNU manual for gcc. I tried to put just basic yet very important concepts of gcc.



Hope that this article might be of some use for the reader. Feel free to put yours comment.

No comments:

Post a Comment