gcc and g++ are GNU’s C and C++ compilers. Features of GCC : portable compiler, that is it runs on most platforms and can produce output for many types of processors.
C and C++ allow direct access to the computers memory hence chances of memory access error is high.
PREPROCESSOR : The preprocessor expands macros in the source file before they are compiled. gcc option -DNAME defines a preprocessor macro NAME. There can be macros with values with input in -DVAR=Value in the command.
COMPILING FOR DEBUGGING : the executable file is simply the sequence of machine code instructions produced by the compiler which by itself is not sufficient for debugging. Compiling with -g option allows the use of a debugger which allows the values of variables to be examined while the program is running.
When a program crashes the OS can write a core file which contains the in-memory state of the program at the time of crash. This file is referred to as ‘core dump‘.
Max. size of the core file : $ ulimit -c .
If the result is 0 then the size can be increased by: $ ulimit -c unlimited
GNU debugger is gd, the further analysis can be done by : $gdb a.out core
$ backtrace command : shows the function calls and arguments up to the current point of execution.
breakpoint : stops the execution of the command and returns the control to the debugger, where the variables and memory can be examined before continuing. command : $ break function_name.
COMPILING WITH OPTIMIZATION : gcc provides a wide range of options to increase speed or reduce the size of the executable file.
(i) Source-level optimization : a). Common Subexpression Elimination : which is very powerful as it simultaneously increases the speed and reduces the program size.
b). Function Inlining : aims at possibilities where function call overhead exceeds with the code of function itself.
(ii) Speed-space tradeoffs : making the executable file small may lead to decrease in speed of execution and vice versa. such as loop unrolling , if the loop count is small and each assignment is independent the compiler may use parallelism on processors to support it.
(iii) Scheduling : in it the compiler determines the best ordering of the individual instructions. Many CPU support pipelining, where multiple instructions execute in parallel on the same CPU.
(iv) Optimization levels : in order to control compilation-time and compiler memory usage, and trade-offs between speed and space for the resulting exe. , gcc provides a range of optimization levels 0-3.
C++ Compilation OPTIONS :
a. ‘-Wall’ or ‘-W’ : include extra warnings relate to member functions and virtual classes.
b. ‘-fno-default-inline’ : if you want to control inlining yourself or you want to set breakpoint since its not possible to set break in inline func.
c. ‘-Weffc++’ : shows warning if code breaks guidelines .
d. ‘Wold-style-cast’ : HIGHLIGHTS use of C-style casts in c++ prgm , such as static_cast , dynamic_cast, reinterpret_cast and const_cast.
Hence GCC is amazing compiler with wide range of options to debug and optimize your code.
Happy compiling ! 🙂 <3