make it rollin!

The compilation process in C language.

Roberto Ribeiro
IT student life
2 min readSep 17, 2020

--

What happens when you type “gcc main.c”? | LaptrinhX
C Compiler Process

What is “gcc”?

First I will explain what gcc is and why we use it.

The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain and the standard compiler for most projects related to GNU and Linux, including the Linux kernel.

Working with C, C + +, Objective C and Fortran, the gcc is able to receive a source code in any of these languages and generate an executable binary program in the language of the machine where you run.

How is the compilation process?

  1. Preprocessing

During compilation of a C program the compilation is started off with preprocessing the directives (e.g., #include and #define). The preprocessor (cpp — c preprocessor) is a separate program in reality, but it is invoked automatically by the compiler. For example, the #include <stdio.h> command in line 1 of filaname.c tells the preprocessor to read the contents of the system header file stdio.h and insert it directly into the program text. The result is another file typically with the .i suffix. In practice, the preprocessed file is not saved to disk unless the -save-temps option is used.

2. Compilation

In this phase compilation proper takes place. The compiler compiles the pre-processed source code into assembly code for a specific processor.

C Compiler process

3. Assembly

In this phase the filename.s is taken as input and turned into filename.o by assembler. This file contain machine level instructions. At this phase, only existing code is converted into machine language, the function calls like printf() are not resolved.

4. Linking

This is the final stage in compilation of “Hello World!” program. This phase links object files to produce final executable file. An executable file requires many external resources (system functions, C run-time libraries etc.). This file is now ready to be loaded into memory and executed by the system.

Resume

To compile a file we type “gcc” in terminal followed by the name of the file where the source code is.

We would do it this way:
vagrant@vagrant-ubuntu-trusty-64: gcc filaname.c

--

--