Monday, May 16, 2011

Get familiar with the GCC compiler


In case you are setting up for your first time a development environment you should probably use the most famous & powerful compiler: GCC . follow this step:
Write in the shell window the following command:
apt-get install gcc
The GCC compiler has a load of features and as programmer you should be friend with.
It’s support major programming languages C,C++, Objective C, Objective C++, Java, ADA and even more…
For a basic compiling you should write:
$ gcc -Wall hello.c –o hello
$./hello

By the way as you may know the compilation process flow is:
preprocessing ->compilation ->assembly->Linking
As a programmer you can stop the process at a specific phase, for example you can decide to compile until the Linking phase (last phase). Using the option ‘-c’ so the output will consists of object files output by the assembler.
In case we are talking about Embedded, I would suggest you look more at the GCC compiler about the Machine Dependent Options (A bunch of machines types are supported : ARC, ARM, AVR, x86,IA-64,MIPS,PowerPC and more).
In case you would like to get know the phases of compilations, follow those steps:


Preprocessing:
gcc -E myProject.c > myProject.i

Compiling
gcc -S myProject.i

in these part the code optimizer kicks in,
so you can compile it with optimization switches, i.e:
gcc -O1 -S ./main.c


Assembling
gcc -c myProject.s

Linking (In this step gcc uses the linker ld, a separate program)
gcc -o myProject myProject.o


moreover you can write either this way:
gcc main.o module1.o module2.o -o my_example

or perhaps this way:

source files into object files, and then after this stage should link them in
separate command.
gcc -c main.c
gcc -c moudle1.c
gcc -c moudle2.c 
gcc main.o module1.o module2.o -o my_example 
 
*very useful option is the -g which directs the compiler to include extra debugging information in its output necessary while using valgrind and gdb.

moreover if you have received a crash (such as segmentation fault), you can easily find the cause for the crash. the operating system should produce a file called 'core'
in the current directory. This core file contains a complete copy of the pages of memory used by the program at the time it was terminated.

Some system are configured not to write core files by default, since the files can be large and rapidly fill up the available disk space on a system.
so if the result of "ulimit -c" is zero you should change it into "ulimit -c unlimited"

(it's well advised to insert it into .bash_profile)




Here are few more options which are quite handy:
-I<dir> - this adds the directory dir to the list of directories searched for #include files. The compiler will search several standard directories automatically. Use this option to add a directory for compiler to search.
there is no space between the "-I"  and the directory name.

-lmylib (lower case 'L') search library named mylib for unresolved symbols
(functions, global symbols) when linking. The actual name of the file will be
libmylib.a, and must be found in either the default location for libraries or in a directory added with the -L flag.

-L<dir> - This adds the directory dir to the list of directories searched for library files specified by the -l (lower case 'L') flag.
Here too, there is no space between the option flag and the library directory name.

No comments:

Post a Comment

About