GCC
GCC: GNU C Compiler---->GNU Compiler Collections
Supported Languages: C, C++, Fortran, ADA, Java and Objective-C
preprocessor->compiler->assembler->linker
• preprocessing (to expand macros)
• compilation (from source code to assembly language)
• assembly (from assembly language to machine code)
• linking (to create the final executable)
separate program: cpp ->gcc -S ->as -> ld
general usage: gcc -c -> gcc
or gcc
gcc -Wall -c hello_new.c //generate main.o object file
gcc -Wall -c hello_fn.c //generate hello_fn.o object file
gcc hello_new.o hello_fn.o -o newhello //link
+ - object file
When compiling with -c option, compiler automatically creats an object file with the same name as source file
An object file contains machine code where any references to the memory addresses of functions (or variables) in other files are left undefined; this allows source files to be compiled without direct reference to each other. The linker fills in these missing addresses when it produces the excutables.
In general, linking is faster than compilation—in a large project with many source files, recompiling only those that have been modified can make a significant saving. That is why we use makefile.
By default, gcc searches the following directories for header files:
/usr/local/include/
/usr/include/
and the following directories for libraries:
/usr/local/lib/
/usr/lib/
Note that you should never place the absolute paths of header files in
#include statements in your source code, as this will prevent the program
from compiling on other systems.
-I : add header file path
-L: add library file path
order: 1: -I -L
2: C_INCLUDE_PATH, LIBRARY_PATH
3: standard default directories
Static libraries are the ‘.a’ files.
+ - When a program is linked against a static library, the machine code from the object files for any external functions used by the program is copied from the library into the final executable.
ls /usr/lib/*.a
/usr/lib/libanl.a /usr/lib/libf2c.a /usr/lib/libl.a /usr/lib/librpcsvc.a
/usr/lib/libBrokenLocale.a /usr/lib/libf2c_i2.a /usr/lib/libm.a /usr/lib/librsMath.a
/usr/lib/libbsd-compat.a /usr/lib/libfl.a /usr/lib/libmcheck.a /usr/lib/librt.a
/usr/lib/libc.a /usr/lib/libfl_pic.a /usr/lib/libnsl.a /usr/lib/libutil.a
/usr/lib/libc_nonshared.a /usr/lib/libg.a /usr/lib/libpthread.a /usr/lib/liby.a
/usr/lib/libcrypt.a /usr/lib/libieee.a /usr/lib/libpthread_nonshared.a
/usr/lib/libdl.a /usr/lib/libImplicit.a /usr/lib/libresolv.a
Shared libraries use the extension ‘.so’, which stands for shared object.
An executable file linked against a shared library contains only a small table of the functions it requires, instead of the complete machine code from the object files for the external functions.
Before the executable file starts running, the machine code for the external functions is copied into memory from the shared library file on disk by the operating system—a process referred to as dynamic linking.
Furthermore, shared libraries make it possible to update a library without recompiling the programs which use it (provided the interface to the library does not change).
+ - Because of these advantages gcc compiles programs to use shared libraries by default on most systems, if they are available.
Alternatively, static linking can be forced with the ‘-static’ option
to gcc to avoid the use of shared libraries when both the static and shared library exist.
The debug option works by storing the names of functions and variables (and all the references to them), with their corresponding source code line-numbers, in a symbol table in object files and executables.
the term segmentation fault refers to the fact that the program tried to access a restricted memory “segment” outside the area of memory which had been allocated to it.
+ -
source code level: subexpression elimination
function inlining
speed-space tradeoff: loop unrolling
lowest level: scheduling
x = cos(v)*(1+sin(u/2)) + sin(w)*(1-sin(u/2))
can be rewritten with a temporary variable t to eliminate an unnecessary extra evaluation of the term sin(u/2): t = sin(u/2) x = cos(v)*(1+t) + sin(w)*(1-t)
for (i = 0; i < 8; i++)
{ y[i] = i; }
y[0] = 0;
y[1] = 1;
y[2] = 2;
y[3] = 3;
y[4] = 4;
y[5] = 5;
y[6] = 6;
y[7] = 7;
The debug option works by storing the names of functions and vari-
ables (and all the references to them), with their corresponding source
code line-numbers, in a symbol table in object files and executables.
‘-O0’ or no ‘-O’ option (default)
-O1 -O2 -O3
-Os This option selects optimizations which reduce the size of an executable.
For most purposes it is satisfactory to use ‘-O0’ for debugging, and ‘-O2’ for development and deployment.
gcc --help gcc --version
gcc -v -Wall calc.c: display detailed information about the exact sequence of commands used to compile and link a program.
The ‘-E’ option causes gcc to run the preprocessor, display the expanded output, and then exit without compiling the resulting source code.
gcc -E calc.c
-M option automatically generate dependency relations
gcc -M hello_new.c
hello_new.o: hello_new.c hello.h
which can be used in makefile
Creating a library
ar: ar cr libname.a obj1.o obj2.o
+ -
Profiling:
gprof: gcc -pg hello.c
gprof a.out
iverson@iverson-laptop:~/gcc_and_makefile/math$ gcc calc.c -pg -o calc
iverson@iverson-laptop:~/gcc_and_makefile/math$ ./calc
The square root of 2.0 is 1.414214
iverson@iverson-laptop:~/gcc_and_makefile/math$ gprof calc
Flat profile:
Each sample counts as 0.01 seconds.
no time accumulated
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
% the percentage of the total running time of the
time program used by this function.
cumulative a running sum of the number of seconds accounted
seconds for by this function and those listed above it.
self the number of seconds accounted for by this
seconds function alone. This is the major sort for this
listing.
calls the number of times this function was invoked, if
this function is profiled, else blank.
self the average number of milliseconds spent in this
ms/call function per call, if this function is profiled,
else blank.
total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.
name the name of the function. This is the minor sort
for this listing. The index shows the location of
the function in the gprof listing. If the index is
in parenthesis it shows where it would appear in
the gprof listing if it were to be printed.
Coverage testing:
gcov:
Identifying files:
file a.out
The file command looks at the contents of an object file or executable and determines some of its characteristics, such as whether it was compiled with dynamic or static linking.
examining the symbol table:
nm
+ -
finding dynamically linked libraries
ldd
iverson@iverson-laptop:~/gcc_and_makefile/math$ gcc calc.c -o calc
iverson@iverson-laptop:~/gcc_and_makefile/math$ ldd calc
linux-gate.so.1 => (0xb7f3f000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7ddb000)
/lib/ld-linux.so.2 (0xb7f40000)
iverson@iverson-laptop:~/gcc_and_makefile/math$ gcc calc.c -lm -o calc
iverson@iverson-laptop:~/gcc_and_makefile/math$ ldd calc
linux-gate.so.1 => (0xb7f15000)
libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7edb000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7d8c000)
/lib/ld-linux.so.2 (0xb7f16000)
iverson@iverson-laptop:~/gcc_and_makefile/math$ gcc -static calc.c -o calc
iverson@iverson-laptop:~/gcc_and_makefile/math$ ldd calc
not a dynamic executable
+ -
getting execution time:
time
iverson@iverson-laptop:~/gcc_and_makefile/math$ time ./calc
The square root of 2.0 is 1.414214
real 0m0.001s
user 0m0.000s
sys 0m0.000s
-o: output file name
gcc -Wall hello.c -o hello // generate executable file hello
gcc -Wall hello_new.c hello_fn.c -o newhello //generate from multiple files
-c: generate object file
gcc -Wall -c hello_new.c //generate main.o object file
gcc -Wall -c hello_fn.c //generate hello_fn.o object file
gcc hello_new.o hello_fn.o -o newhello //link
-lNAME: link the libNAME library file.
gcc calc.c /usr/lib/libm.a -o calc
or gcc calc.c -lm -o calc
or gcc calc.c -o calc
-I: add header files path
-L: add library files path
-g: debugging
gcc -g hello.c
ddd a.out
-O1 -O2 -O3: optimization
-E: preprocessor result
-M: dependency report