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 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:
gcc main.c -o main -L /path/to/libraries -libexample.so or example.dll
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:
gcc app.c -o app
This will do all the steps discussed above and generate an executable (ELF) file named app.
Useful GCC options
-Wall β enables all warnings.
-E β shows the code that preprocessors use.
-S β only assembly code.
-save-temp β save all the .o files (produce everything).
-l β link to a file.
-lm β for math library while using -l (link to math library).
-fPIC β create a shared library ( then use -shared -o prog.so prog.o).
-g β generates debugging info.
-v β verbose on every step.
-ansi β make sure thing are compatible with the ansi standard.
-funsigned-char β char type is threated as unsigned type.
-fsigned-char β the aposite of the above options.
-DMY_MACRO β define MY_MACRO.
-Werror β all warnings will be threated as errors.
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:
-masm=intel or -masm=att.
βOptimization Flags
-O β tries to reduce code size and execution time without performing any optimizations that make a great deal of compilation time.
-O1 β takes more time and a lot more memory.
-O2 β optimize even more. performs nearlly all supported optimizations that do not involve a spade-speed tradeoff.
-O3 β turns on all optimizations.
-Ofast β turns on everything including the optimizations that are not valid for all standard-compilant programs.
-Og β optimize debugging experience.
Strip and reduce binary size
-Os -> this flag enables size optimization, focusing on reducing the size of the resulting binary. It applies various optimization techniques to achieve smaller code size.
-s -> this flag tells GCC to strip symbols from the compiled binary, reducing its size. This makes it harder to analyze the binary and extract meaningful information from it.
-flto -> this flag enables link-time optimization, Link-time optimization can help reduce code size by eliminating redundant code and optimizing function calls.
-ffunction-sections and -fdata-sections -> these flags enable the generation of individual sections for each function and data object.
--gc-sections -> removal of unused functions or data sections during the linking phase.
-Wl and --gc-sections -> this linker flag removes unused sections from the final binary, further reducing its size by eliminating unused code and data sections.
These are some of the most used gcc compilation options that you will usually use, for more info and options refer to .