Olivier Guyot
8 min readSep 16, 2020

--

If you are here, it’s because you click on my Linkedin post good job ! you do the good things and you will learn some interesting things.

How to compile a file main.c written in C ? OF course with gcc !! BUT what happens?

If you do C , of course you have already compile your file. Lets go deep in the command.

I know i have already talk about the terminal, but a little refresh can be useful,

Let’s go to the begining. The Terminal !

When we open the terminal, our shell will check the PS1. Don’t worry about that word, it’s just the primary prompt which is displayed. ( hint, if you want to be the BEST for you friend, you can personalized you prompt by modify it. Their is different prompt PS1, PS2 ,… . By modifying these variables, like the PS1. You can personalised your prompt at any level, you can change the form, the color, …or do what ever you want.

Yeah ! you are the boss !

That is my PS1 first prompt

Then, he is waiting a command ! ( hint, if you want to display your PS1 just type echo $PS1.

WOOOH, it looks complicated. AHAH yes a little bit but if you want more information about how to change your prompt, i will maybe do an article en that later, but for now it’s not the subject.

So our terminal display a prompt, and it will display the prompt after each command ! because he is waiting to speak with us.

It’s our time to speak. YEAH, now you can put some command in the terminal.

If you put a command, the terminal will first check if there is a what we call an aliase.

What’s an aliases?

“An aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands.”

So, when we put a command our shell will first check if there is some aliases to replace it.

In the common live, an aliases it like a word shortcut,

Prompt? OK, Aliases ? OK, and now ?

Now there is the use of a command. all the command of our shell are specified in a specifics directories, The location of these directories are specified in the PATH environmental varaible.

Here an example of a PATH.

So all our command are in a directory of my computer and i can call them?

YES!!! you get it.

Now, the shell will read the directory specified in the path variable and will see if the command existe. if it existe, it will use it. As gcc is an existing command, it will work.

And GCC ?

gcc is a special command which call a compiler . the “GNU Compiler Collection” and it’s is part of the GNU project.

According to Wikipedia, “The GNU Project is a free software, mass collaboration project that Richard Stallman announced on September 27, 1983. Its goal is to give computer users freedom and control in their use of their computers and computing devices by collaboratively developing and publishing software that gives everyone the rights to freely run the software, copy and distribute it, study it, and modify it. GNU software grants these rights in its license.”

GCC is not dedicated to only c and it’s a portable software that you can find in lot of different machine.

In order to use gcc we will write gcc <the name of a file> if we write the program in c, we will generally have the .c at the end.

I GET IT, so to compile a file main.c written in c, i will use gcc main.c

What’s a compiler ? Before answering lets first see how can we speak to a computer

There is different way to speak to our computer. But he is not really as clever as you and he can’t speak a lot of different languages, he generally speaks the : Binary language.

A binary language is a 2 bases languages with only 2 charactere 01. If you need more information about bases tell me and i will explain it to you, but it’s not the actual subject.

For human like you and me, a binary language is really not easy to understand that’s why we need a translator. It’s the origin of a Compiler. A compiler software will translate a language to another language.

For example he can translate C to Binary,

I GET it then, my computer will understand it?

YOU ARE RIGHT !!! We will write in a language that a human can write and understand and then the compiler will translate it into an language that my computer can understand.

How does gcc work to do the translation?

In fact it’s not so simple because when we write a programme , it’s not written in only one book and in an easy way and in only 1 languages. That why we need four steps in the GCC compilation process : the preprocessor, the compiler, the assembler, the linker.

In our example we are talking about the gcc main.c

You know that in a C language generally there is different part of a program that are usefull for human but not for a computer. Our compiler will deal with them. in the first Stage.

Stage 1: Preprocessor

GCC will take the source code and then, will remove comments ( a comment is an information written for an other human), header ( a way to access to some other programme. A set of program is called library), and it will replace all the macros ( a small part of a code) by their values,

In order to display what do the gcc at that level you can use the -E flag and it will stop gcc at that first step.

For example if i have a programme like that :

And if i use gcc -E it will have

Some information has been added and # in some lines. Don’t worry about it now it just mean they will be use after.

If i go down in my file i arrive to the last part.

I see , after main it’s my program !

Lets go to stage 2 : The compiler.

Lets beging the translation. In order to translate into a language that our computer can understand we will use an intermediate language or IR ( intermediate representation). It’s an expand of the pre-processor in order to convert the code into assembly code.

An assembly code is a code which depends of the machine. Every assembly language is designed for exactly one specific computer architecture.

Example of an old assembly code from my friend wikipedia.

wikipedia assemble language for Motorola 6800

I GET IT , so assembly code is here because all of the machin in the world don’t speak exactly the same language but they speak binary?

Don’t go too fast my friend, but you are right they don’t speak the same language. We could say more or less that they use the same alphabet ( binary ) but not the same language. Like us if you only speak English, it’s hard to understand “le Francais”.

Let’s go back to our compiler. In order to stop a compiler , we can use de gcc -S command.

And the main.s file will be created.

Example of a file .s after using the gcc -S command

Here, the pre process code is translate into an assembly code which will be use at the third step,

Third step THE ASSEMBLER

The assembly code is converted into a machine code

I KNOW my computer can understand binary ?

Yeah you get it, you are good !

So like the other step you can use a command to stop the program at that level:

And finally it will create a compiled file.

Hey it’s not made with 0 and 1 it’s not binary my computer can’t understand.

You are right again ! after that step, our computer is clever and can translate letter and number into his binary number correspondance !

SO IT S FINISH?

NO It’s not the last step is LINKER

As we have seen our program can use some library. but we have not talk about it for now. In order to make these library available, we have to link them to our program. So Basically it’s at this step that all link are created.

We will use the command gcc and our file will be created. ( without any option it will be called a.out )

NOW YOU HAVE YOU COMPILED FILE

It’s incredible because there is everything in your compiled file, the program, the link, and it’s understandable by your machine and personalised for her. Generally the file will be name a.out.

Then you can type on your terminal a.out in order to launch your program

CONGRATULATION And thank you GCC. I did not mention id, but gcc is clever and can give you some information if you do some mistake , he will help you to correct yourself.

Example of error message

--

--