What happens when you type gcc           main.c ?
The GNU project and its compiler GCC (GNU COMPILER COLLECTION).

What happens when you type gcc main.c ?

GCC is an integrated compiler of the GNU project for C, C++, Objective C and Fortran. This compiler is capable of receiving code from these languages and generates a binary executable program in the language of the machine where it will work. GCC stands for (GNU COMPILER COLLECTION) and until today it is still used for this functionality.

So basically, a compiler is a translator that transform a program into a language that machines can understand. The compilation has a series of steps to get an executable file, these steps can be make it individual or all in one step. In this post will work again on Linux, so the compilation can be done by commands.

The steps that compiler does...

  • Preprocessing
  • Compilation
  • Assembly
  • Linking


Prepocessing:

At this stage the #define variables are interpreted and replaced where they were called.

No alt text provided for this image

Above we have a program in C with a sum a constant named A, that makes a sum between A and b, and prints the result in sum, we will do the preprocessing stage to prove the statement above:

No alt text provided for this image

That command make the preprocessor, lets see what happens behind...

As I said before, the preprocessor replaced the #define variables, we can see the below that its happens:

No alt text provided for this image

Executing the command cat, we can see within the pre-example file first show what's in "<stdio.h>" (a lot of functions) and later replace all #define (constant variables, in this case).


Compilation:

Compilation transforms C code into assembly language, in that way the machine can understand the code. WE use the same example to do the step, using the this command we can produce the assembler code "gcc -c example.c"

Using cat to explore inside the file, we can see the assembler code:

No alt text provided for this image


Assembly:

The assembly transforms the assembly language into a object code, using the command "gcc -S example.c" we can make the assembly step:

No alt text provided for this image

Note: if you use the command above, a .s extension is created within that file is the object code.


Linking:

This step incorporates the object code in some executable file, using the command "gcc example.c -o exe-example", lets see below:

No alt text provided for this image

The file created was exe-example and we can execute using the next syntax "./file_to_execute".

You could see the compilation process, most of the time you only need the executable file, but if you have an issue, maybe checking with some of those steps you can find the issues.

要查看或添加评论,请登录

Juan Uribe的更多文章

  • what is IoT?

    what is IoT?

    In simple terms, The Internet of Things (IoT) refers to the constant tendency to connect all kinds of physical objects…

  • Machine learning!

    Machine learning!

    First of all the context, everyone talks about machine learning so we will do it too! This is a subfield of computing…

  • Python3: Mutable, Immutable... everything is object!

    Python3: Mutable, Immutable... everything is object!

    Brief introduction! Object Oriented Programming (OOP or OOP) is a programming paradigm, Python is a language OOP—what…

  • Dynamic libraries!

    Dynamic libraries!

    Why use libraries in the programming world? A good way to understand why use libraries is when we are making a program…

  • What happens when you type ls -l in the shell

    What happens when you type ls -l in the shell

    This post describes step by step how a command line interpreter works, when the command ls -l is typed in the command…

  • Static libraries!

    Static libraries!

    Why use libraries in the programming world? A good way to understand why use libraries is when we are making a program…

  • What is the difference between a hard link and a symbolic link?

    What is the difference between a hard link and a symbolic link?

    Consider the following scenario: There is a file deeply buried in the file system called…

  • What happens when you type ls *.c

    What happens when you type ls *.c

    The commands on linux are reserved words that the Operative System uses to execute determined actions using a terminal…

社区洞察

其他会员也浏览了