What happens when you type gcc main.c?

gcc?… main.c?… C??? What are those strange words?

So… first, gonna explain this topics:

What is GNU

GNU is a free software operating system of the UNIX type. Created in 1984 and generally used in conjunction with the Linux kernel, in GNU / Linux distributions.

But, what is UNIX?

It is an operating system initially created to manage servers, where the main means of interaction is through commands, it is proprietary software created by AT&T Bell Labs, making it a system that cannot be edited. GNU is based on the UNIX system, since in the same way it was created to be used mainly from the command line, its main difference is that it is completely free, so it respects the 4 freedoms of that philosophy: it is possible to use , study, copy and improve your source code.

What is gcc?

GCC is an integrated compiler of the GNU project for C, C ++, Objective C and Fortran; it is capable of receiving a source program in any of these languages and generating a binary executable program in the language of the machine where it has to run.

Below are some of the options of gcc

Img 1. gcc — help

What is lenguage c?

Also known as “Systems Programming Language” developed in 1972 by Dennis Ritchie for UNIX a multiplatform operating system. The C language is of the structured language type such as Pascal, Fortran, Basic. Its instructions are very similar to other languages including statements like if, else, for, do and while …. Although C is a high-level language (since it is structured and has statements and functions that simplify its operation) we have the possibility of programming at a low level (as in the Assembler playing registers, memory, etc.). To simplify the operation of the C language, it includes libraries of functions that can be included by referring to the library that includes them.

Well, now goes to answer the main question on this blog

So…

What happens when you type gcc main.c?

First suppose we have one file called main.c that prints the message “Hello World”

Img 2. main.c

So to compile this file four successive stages must occur: preprocessing, compilation, assembly, and linking. To go from a human-written source program to an executable file, it is necessary to carry out these four stages in succession.

Step 1. Preprocessing

At this stage the directives to the preprocessor are interpreted. Among other things, variables initialized with #define are replaced in the code by their value wherever their name appears.

This is done with the next command:

gcc -E main.c

Img 3. Preprocessing

Step 2. Compilation

The compilation transforms the C code into the assembly language of our machine’s processor.

Img 4. Compilation

This command performs the two first steps and creates the file main.s

With the command more main.s we can see the program in assembly language:

Img 5. Assembly language

Step 3: Assembly

The assembly transforms the program written in assembly language into object code with extension .o, a binary file in machine language executable by the processor.

This can be done with the command:

Img 6. Command to assembly

This creates a file calls main.o

With the command file main.o we can see the type of this file

Img 7. Type of main.o

Step 4: Linking

In this step one or more modules in object code are brought together with existing code in libraries.

To complete this step, gcc is provided with the object codes (files .o).

Img 8. Linking

This command creates a executable file, which can be executed as follows with the command ./main:

Img 9. Executing main

Finally all this steps can be done with only one command, which is:

Img 10. All steps in one command

but it is only useful if we have a single source file.

Also with the option -v in gcc a detailed report of all the steps of the process is obtained.

Img 11. Verbose option in gcc

So… this is all the proccess of how a C program is compiled. Step by step, I hope you have learned something new with this post.

Thank for your attention, and have a nice day.

Hello World!