GNU Compiler Collection

--

First of all. The GNU project is a mass collaborative initiative for the development of free software. Richard Stallman founded the project in 1978 at MIT. The original purpose of the GNU project was the creation of a free operating system. Free, in a software context, does not necessarily mean free of cost.

Image taken from https://en.wikipedia.org/wiki/GNU

The GNU Compiler Collection (GCC) is an optimizing compiler, ( tries to minimize or maximize some attributes of an executable computer program), produced by the GNU Project that supports various programming languages, hardware architectures, and operating systems. The Free Software Foundation (FSF) distributes GCC as free software under the GNU General Public License (GNU GPL). GCC is a key component of the GNU toolchain and the standard compiler for most projects related to GNU and the Linux kernel.

The compiler is software that converts a program written in a high-level language (Source Language) to low-level language (Object/Target/Machine Language).

Taken from https://github.com/topics/compiler-theory

¿What goes inside the C compilation process?

# Pre-processing

This is the first phase through which source code is passed. This phase include:

  • Removal of Comments
  • Expansion of Macros
  • Expansion of the included files.
  • Conditional compilation

Macro expansion are patterns and templates define rules for transforming text expressions, in the style of languages, in the case Ck. In the macro expansion, these rules are applied and the macro call is transformed into an expression resulting from its application. Conditional compilation directives help to compile a specific part of the program or allow us to skip the compilation of some specific part of the program based on some conditions.

Taken from https://www.examtray.com/tutorials/last-minute-c-programming-preprocessor-directives-tutorial

# Compiling

The compiler analyzes the syntax and semantics of the preprocessed source code and translates it, generating a file containing the object code. The default extension of these files is .o (GNU) or .OBJ . The GNU C Compiler, can be used to perform compilation only (building object modules) if the -c parameter is specified in its call. For example, given a source module called test.c, the corresponding object module is built by running: gcc -c -o test.o test.c .

This is achieved by calling gcc without the -c argument: gcc’s default behavior is to create an executable, that is, compile and link. In the compilation process, the source code is interpreted except for references to external objects (functions or variables) in the code module that is being compiled. These external objects, especially functions, are declared in this module, but are defined in others. The declaration will help the compiler to check that the external references are syntactically correct. After compiling it generates an intermediate code in assembly language as <file-name.s> file. It is assembly version of our source code.

Let us look into compilation.s file.

# Assembly

In this phase the filename.s is taken as input and translates to low level machine code (filename.o) by assembler, the function calls like printf() are not resolved. After successful assembling it generates <file-name.o> (in Linux) or <file-name.obj> (in Windows) file known as object file, wich is encoded in low level machine language and cannot be viewed using text editors.

How *.o file looks like

# Linking

Finally, the linker resolves references to external objects found in a source file. These references are to objects found in other compiled modules, either in the form of object files or built into some library. The GNU-supplied linker program is ld. Besides, Linker adds some extra code (commands) to our program which is required when the program starts and ends. Through these commands, we know that how output file increases from an object file to an executable file.

Summary c compilation process

And this is the back stage of the daily use that we do every time we compile our programs in c. The next time we wildly compile our code, we may be more aware of how this translator allows us to tell the computer to do something.

References

--

--