Compile with gcc

Arquimedes
4 min readJun 11, 2020

¿Do you know what is a compiler?

It is a software that translates a program written in a high-level programming language (C / C ++, COBOL, etc.) in machine language. A compiler generally generates assembly language first and then translates the assembly language into the machine language. A utility known as a "linker" combines all the necessary machine language modules into an executable program that can be run on the computer.

Lets talk about GCC Compiler

GCC is the GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Ada, Go, and D, as well as libraries for these languages (libstdc++,…). GCC was originally written as the compiler for the GNU operating system.

¿How it works?

The compilation process involves four successive stages: 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. The gcc and g ++ commands are capable of performing the entire process at once.

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.

Compilate gcc main.c

Star with the compilate process, creating the file main.c, .c is the file extension for the process written in c.

1. Preprocessor

The source code is the code which is written in a text editor and the source code file is given an extension “.c”. This source code is first passed to the preprocessor, and then the preprocessor expands this code. After expanding the code, the expanded code is passed to the compiler.

You can type “-E” with the gcc command on the source file, and press enter.

gcc -E main.c
This is what you look after type gcc -E main.c

2. Compiler

The code which is expanded by the preprocessor is passed to the compiler. The compiler converts this code into assembly code. Or we can say that the C compiler converts the pre-processed code into assembly code.

In the compiler process, you can type “-S” with the gcc command on the source file, and press enter.

gcc -S main.c

After you do this, the file main.s is created and looks like this:

3. Assembler

The assembly code is converted into object code by using an assembler. The name of the object file generated by the assembler is the same as the source file. The extension of the object file in DOS is ‘.obj,’ and in UNIX, the extension is ‘o’. If the name of the source file is ‘hello.c’, then the name of the object file would be ‘hello.obj’.

gcc .-c main.c 

After this the file main.o is created:

4. Linker

Mainly, all the programs written in C use library functions. These library functions are pre-compiled, and the object code of these library files is stored with ‘.lib’ (or ‘.a’) extension. The main working of the linker is to combine the object code of library files with the object code of our program. Sometimes the situation arises when our program refers to the functions defined in other files; then linker plays a very important role in this. It links the object code of these files to our program. You can type this to create the executable file:

gcc main.c

In DOS, the extension of the executable file is ‘.exe’, and in UNIX, the executable file can be named as ‘a.out’. For example, if we are using printf() function in a program, then the linker adds its associated code in an output file.

So now we could either type “./a.out” if you didn’t use the -o option or “./my_program” to execute the compiled code, the output will be “Hello, World”, and following it the shell prompt will appear again.

After this process, we can already type “./a.out” or -o or “./example”, and thus execute the compilation, the output will be “Hello, World”.

So you ready to do your first gcc main.c ?

Go ahead and try!

--

--

Arquimedes

Beginner Programar, social comunicator. Love pop music, family and friends.