What happens when you type gcc main.c?

Andrés Sotelo
3 min readSep 17, 2020

First of all, we must understand some things to get a clear context about gcc. I want to start by defining, what is C and what is a compiler.

What is C?

(C is one of the most used programming languages all over the world together with Python, Java and C++)

C is a programming language developed by Dennis Ritchie in 1972, it´s a extremely popular language because is simple and flexible, it´s an imperative procedural language and it was designed to be compilated (this is our keyword). C is a general-purpose programming language and is the basis to write everything from O.S like Windows and many others. We can say that C is the base for the programming and if we know and understand C, we will be able to learn many other languages that has a “C-pedrigree” and uses the concept of C.

We said that C is a language designed to be compilated, right? But what does this means? This means that everything you write in your C program, must be processed by a compiler to make it an executable program that a computer can run (execute).

What´s a compiler?

The definition of a compiler looks easy, is a special program that processes statements that were programming in a particular language and turns then in a executable program or code that a computer can process and execute. In other words, a compiler convert a human readable code format into a machine readable code (binary code).

What´s gcc?

Compilation process

The current official meaning of GCC is “GNU Compiler Collection”, which refers to the complete suite of tools to compile mainly C and C++ language programs into binary code and create a executable program.

The compilation is a four-steps process, which will be explained below: suppose we have a file named main.c (we know this is a C file because have .c extension).

$ gcc main.c

  1. Preprocessing

This is the first step in the compilation process, where gcc rendering C code and ensures that only what is necessary is present. Removes all the comments, includes code from header and replaces macros by their values.

2. Compilation

In this step, the compiler takes the output of the preprocessor process and generates assembly code.

3. Assembly

The assembly code generated into compilation process it´s going to converted into a machine code (i.e. binary )

4. Linking

This is the last step, the gcc creates an executable file called a.out that we can run by typing “./a.out” in the command line. We can also create an executable file with the name that we want, by adding “-o” option.

You can read about all options that you could to combine with “$ gcc” in the man page (man gcc).

If you liked the content of this post, please clap!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response