GCC Compilation
GCC (upper case) refers to the GNU Compiler Collection.
GCC is an open source compiler suite which include compilers for C, C++, Objective C, Fortran, ADA, Go and Java.
gcc (lower case) is the C compiler in the GNU Compiler Collection.
The GCC project has detailed documentation at https://gcc.gnu.org which document installation, general usage, and every command line option. Please refer to the official GCC documentation on any question not answered here.
Compilation steps
1) Pre-processing
The preprocessor obeys commands that begin with "#" (known as directives) by:
Removing comments.
Expanding macros.
Expanding included files.
If you included a header file such as #include <stdio.h>, it will look for the stdio.h file (usually in /usr/include
directory) and copy the header file into the source code file.
The preprocessor also generates macro code and replaces symbolic constants defined using #define with their values.
2) Compiling
It takes the output of the preprocessor and generates assembly language, an intermediate human readable language, specific to the target processor Intel, MIPS, ARM etc...
There are several assembly syntaxes for every processor, I will be using Intel x86 syntax but worth noting others such as AT&T widely used with the GNU Assembler (GAS) and commonly found in Unix-like systems.
3) Assembly
The assembler will convert the assembly code into pure binary code or machine code (0s & 1s).
This code is also known as object code that have an extension of ".o".
4) Linking
The linker merges all the object code from multiple modules into a single one.
If we are using a function from libraries, linker will link our code with that library function code.
Static Linking
Static linking means putting all the necessary code from libraries directly into the executable file. When you compile your code, the linker copies the required library functions into the final executable.
This makes the executable bigger, but it can run on its own without needing any additional files.
If your program needs to calculate square roots using sqrt() from #include <math.h>, static linking will include the code for sqrt() directly in the executable.
Dynamic Linking
Dynamic linking involves referencing external libraries by name, and the actual library code is loaded and linked at runtime.
If your program uses a library called "libexample.so" or "example.dll" for certain functions, dynamic linking will only include references to those functions in the executable.
The actual library is loaded when the program is executed.
If you use a non-standard library, you must specify its location by:
Basic GCC syntax
Here is a basic usage of gcc for compiling a non-threaded C code with no external linking and libraries (except for standard C libraries) on linux systems:
This will do all the steps discussed above and generate an executable (ELF) file named app.
Useful GCC options
These are some of the most used gcc compilation options that you will usually use, for more info and options refer to gcc official documentation.
C and disassembly
βThe gcc option -O enables different levels of optimization.
Use -O0 to disable them and use -S to output assembly.
-O3 is the highest level of optimization.
Starting with gcc 4.8 the optimization level -Og is available. It enables optimizations that do not interfere with debugging and is the recommended default for the standard edit-compile-debug cycle.
To change the syntax of the assembly to either Intel or AT&T use:
βOptimization Flags
Strip and reduce binary size
Last updated