What happens when you type gcc main.c

Aucun texte alternatif pour cette image

When we want to execute our C code, the computer needs to read it in machine language, which is (base-2) binary code in order to understand it and execute it.

The gcc or GNU Compiler Collection is a collection of programming compilers that can translate our C code into Machine language so that the computer can execute our files.

But how does that actually happen?

Aucun texte alternatif pour cette image

A compiler (gcc) has 4 modules: preprocessor, compiler, assembler and linker

When we write the file main.c, the preprocessor generates some intermediate file, that file is given to the compiler. The role of the compiler is to take the file generated by the preprocessor as its input and convert it into the assembly language.

Though, assembler code is still not understood by the computer. It needs to be converted into machine code. The converter that does this job is the assembler. The assembler module will convert the assembly code into the object code.

Lastly, the linker, the last module, links the object code (created by the assembler) with library functions code that we use (when we write our code) and generates executable files.

1. Preprocessor

When we run gcc main.c, main.c program gets processed by preprocessor. There are three things that preprocessor does:

  • First: it removes all the comments from our program main.c
  • Second: it includes code from header and source file
  • Third: it replaces macros (if there are any that are used in the program) with code.

2. Compiler

The code generated by preprocessor is then given to compiler and compiler generates assembly code from it.

3. Assembler

Since our computer still can not understand assembly code, the assembler converts assembly code into binary code (the one our machine can actually interpret).

4. Linking

Now linker is the one that links all this binary code form our program to libraries and puts it together into one code that we get in a form of executable file (a.out).

But of course we don’t want to have all of our programs be named the same, do we? So we can use -o option with our gcc command that is allowing us to name our executable file whatever we want 

Necessary Linux commands

  • To run a C code, you type gcc filename. For example, gcc main.c
  • If you want your output to be in a certain file, then type gcc filename -o outputfile. For example, gcc main.c -o cinfun
  • In order to run your C file through the preprocessor and save the result into another file, type gcc -E filename -o your_output_file. For example, gcc -E main.c -o cisfun.
  • If you want your C file to compile but do not link, type gcc -c filename. For example, gcc -c main.c (your output file will be main.o).
  • In order to generate the assembly code of your C code and save it in an output file, type gcc -S filename. For example, gcc -S main.c (your output file is supposed to be main.s)


Richard East

Chief Engineer (ATE) at Cobham AvComm (Aeroflex) a Cobham Company

5 年

"Now?linker?is the one that links all this binary code form our program to libraries and puts it together into one code that we get in a form of?executable file?(a.out)." Hi Mariem, The "form" should be "from" .... but very good article!!!

要查看或添加评论,请登录

Mariem Matri的更多文章

社区洞察

其他会员也浏览了