64Chapter3 • Compilers and Assemblers
Here I have used GCC version 2.96 that came with RedHat Linux 7.1 and you can see
directories and references to this version information also.
You can also find the amount of time taken by each process during compilation. The fol-
lowing command displays time taken during each step of building the output file.
[rr@conformix 4]$ gcc -time hello.c -o hello
# cpp0 0.06 0.00
# cc1 0.08 0.01
# as 0.02 0.00
# collect2 0.12 0.03
[rr@conformix 4]$
It is also evident from the output of the above command that GCC has used four other pro-
grams (cpp0, cc1, as and collect2) during the compilation process.
3.3.5Compilation with Optimization
The first objective of a compiler is to generate output code swiftly. The compiler does not
do any code optimization to make the compile time short. However you can instruct
gcc
to
compile code with code optimization. This is done using –
O
(uppercase O, not zero) on the com-
mand line. Different optimization levels can be designated by using a number suffix with this
option. For example,
-O2
will do code optimization at level 2. If you specifically don’t want to
do any code optimization, you can use zero with option as –O0.
So what does optimization mean. Consider the following C source code file
sum.c
that
calculates the sum of two numbers and prints the result. Of course this is not the best code for
this purpose and it is used only to demonstrate a point.
1 #include
2 main ()
3 {
4 int a, b, sum;
5
6 a=4;
7 b=3;
8 sum = a+b;
9
10 printf("The sum is: %d\n", sum);
11 }
If you compile this program without any optimization, the compiler will generate code for
all lines starting from line number 6 to line number 10. This can be verified by loading the file in
a debugger and tracing through it. However, if you optimize the compilation process, lines 6 to
10 can be replaced by a single line as shown below. This can be done without affecting the out-
put of the program.
printf("The sum is: 7\n", );
Next Page >>
<< Previous Page
Back to the Table of Contents