Compiling C code

Jose Uribe
3 min readFeb 8, 2021
Source

C is one of the most widely used programming languages today. Since its creation between 1972 and 1973 by Dennis Ritchie, it has been a constant love-hate relationship between its users.

My coding life started many years ago learning the basics of HTML and CSS by myself. It was something interesting for me, but nothing mind blowing. As time went by, I became interested in learning more in depth. I continued with JS, but it didn’t grab me. Until I finally met Python, it was love at first sight.

After years of running away from it, I came up against it again. C the intermediate level programming language, which I always found boring and complicated. Now i have to learn about it. So here i am.

Today we gonna learn what happens when you write some lines in C and then run gcc command to it. In a simple and straightforward way.

Compiled vs Interpreted

In a simple way. They are two kinds of programming languages: compiled vs interpreted. With an interpreted programming language, like Phyton… you only need to write it and then run it. And it will works, well… if you didn’t do a mess. But in the other hand, with a compiled programming language like C. You need to write it, then compiled it and then you can run it.

Why you need to do that? Well, if you have a bit of experience with the coding world, you would know that computers don’t speak our language. They do everything with binary code or machine code. Circuits of energy that turns on and off at super high speeds.

When you use a compiled programming language, you uses something called interpreter that translates in real time all the lines of your code into binary code.

When you compile some lines of code, a thing called the compiler -very wise name- translate that lines to machine code and put them into an executable file.

That’s, in a few words. What gcc does. Translate your lines of C into an executable file.

What happens between?

When you run: gcc main.c -o mainOutput on your console, you are basically running four process:

Preprocessing

That’s when the compiler removes all the comments of the code, copy the headers that you #include on your code to the main.c file, and replaces symbolic constants defined with their values.

Compiling

It takes the output of the preprocessor and translates to assembly language.

Assembly

The assembler convert the lines compiled into binary code.

Linking

This is the final step, where the compiler links all the libraries used in your code. If they are static, the compiler will copy all the libraries on the final file and if they are dynamic the compiler will place the name of the library in the binary file.

At the end you will have two files in your directory:

main.c -the code- and mainOutput -the executable file-.

If your run gcc main.c all the process will be done buy the executable file will be a.out. If you are running gcc on Windows, the extension of the executable file must be .exe

That’s basically all you have to need to know when compile C code. If you want to know more in depth, feel free to Google it.

--

--

Jose Uribe
0 Followers

Platzi Master C8 Student, learning a lot every day.