The GNU Compiler Collection is a set of compilers for various programming languages produced by the GNU Project. GCC has been adopted as the standard compiler for most of the Unix-like operating systems, including Linux, BSD, and Mac OS X.
GCC is one of the first software packages developed by the Free Software Foundation within the GNU Project (Gnu’s Not Unix). The GNU Project was initiated as a protest against proprietary software by Richard Stallman in the early 1980s.
At first, GCC was translated as “GNU C Compiler“, because initially, the purpose of the GCC project was to develop a portable C compiler on UNIX platforms. Subsequently, the project evolved today as a multi-backend compiler with support for C, C ++, Objective-C, Fortran, Java, and Ada. As a result, the GCC acronym means today the “GNU Compiler Collection“. At the impressive number of languages above, we also add a large number of supported platforms in terms of hardware architecture (i386, alpha, vax, m68k, sparc, HPPA, arm, MIPS, PowerPC) (GNU/Linux, DOS, Windows 9x /NT/2000, BSD, Solaris, Tru64, VMS). Currently, the GCC is the most portable compiler. GCC uses gcc or g ++ for compiling C/C ++ programs. A typical invocation is for compiling a program from a single source file:
$ gcc hello.c $ ./a.out Hello, world!
Other useful options are:
1. Lcale – this option instructs the compiler to look for the libraries that the program should use in the path directory; the option can be specified several times to add more directories
2. Library instructs the compiler that the program needs the library. The file containing the library will be called library.so or library.a.
3. Icale – instructs the compiler to look for header files and directory paths; the option can be specified several times to add more directories
4. Onivel-optimizations – installs the compiler with what level of optimization should be applied;
-O0 will cause the compiler not to optimize the generated code;
-O3 will cause the compiler to optimize the generated code to the maximum;
-O2 is the threshold from where the compiler will begin to insert inline functions directly into the code instead of calling them;
-Os will optimize the program to reduce the size of the generated code and not the speed.
5. -g – If this option is used, the compiler will generate in the output files information that can then be used by a debugger (information about the source files and a mapping between the machine code and the source code lines).
The examples so far deal with written programs in a single source file. In reality, applications are complex and writing the entire code into a single file makes it hard to maintain and difficult to expand. In this regard, the app is written in several source files called modules. A module typically has functions that fulfill a common role.
Compilation phases:
1. Preprocessing. Processing involves replacing the preprocessing directives in the source file C. The preprocessing directives start with #.
2. Compilation. The compilation is the phase in which the preprocessed file gets a file in assembly language.
3. Assembling. The assembly is the phase in which the written code in assembly language is translated into machine code representing the binary encoding of the initial program instructions. The file obtained is called the object code file, it is obtained using the -c option of the compiler and has the extension .o.
4. Editing links. To get an executable file it is necessary to solve the various symbols present in the object file. This operation is called link editing, link-editing, linking or linking.
A library is a collection of precompiled features. When a program needs a function, the linker will call that function from the library. The file name of the library must have the prefix lib:
Unlike a static library that I’ve seen is nothing but an archive of object files, a shared library is itself an object file. Creating a shared library is done through the linker. The -shared option tells the compiler to create a shared object rather than an executable file.