The Process of Compiling a C Program

The Process of Compiling a C Program

In this article, I will talk about the process of compiling a C program. Before we get started, I will brief what C language is and what gcc is, then it will help to understand the compiling process better.

What is C?

C is a programming language which appeared in 1972 firstly and its inventor is Dennis Ritchie.  C is also a compiled language, unlike interpreted language, this means C’s source files will be complied to be executable.

gcc

While talking about the compilation of C program, we definitely need to mention gcc, which is the command to invoke the GNU C Compiler to compile the C code and produce object code. In other words, it takes the source code, decomposes it, generalizes the structure, restructures it, optimizes it and then converts it into machine code. 

No alt text provided for this image


To get the compiling process started, you need to run below command – 

$ gcc source.c

After we run gcc command, there are 4 steps during the whole compiling process, see below image -

No alt text provided for this image

Step 1 – Preprocessing

There are mainly three things happened in this step – 

·      Remove all the comments, as comments help people to understand the code, but machine doesn’t need them.

·      Expand the macros. For example, in the source file, we define some macros, the preprocessor will replace the symbolic constants with their values.

·      Expand the included files. If there is a header file such as # include <studio.h>, it will copy the header file into the source code file.

The output of this step is an expanded source code with the extension “.i”. For example, the file name will be “source.i”.

We can use “gcc -E source.c” to stop the compilation, so we have a look what the “source.i” file look like.

No alt text provided for this image

Step 2 – Compiling

The compiler will convert the “source.i” to “source.s” file which is in assembly level instructions. By saying that, I mean this file gets translated into intermediate code which can be understood by CPU. Also, it will check this file for syntax errors and optionally optimize the code for better performance. We can use “gcc -S source.c” to have a look the “source.s” file. 

No alt text provided for this image


Step3 – Assembly

The assembler takes the intermediate code and convert it into object code, which is in machine language, for example, binary. This is totally unreadable for human beings. The output of this step will be stored in the “source.o” file. Use “gcc -c source.c” to get the “source.o” file.

No alt text provided for this image

Step 4 – Linking

The final step of compiling is to link all the objects files and libraries together and combine them and then produce a single file, normally this file will be able to execute.

·      Link all the source files such as all other object codes in this project, or we can say that other object code will be linked to this “source.o” file.

·      Link the calls of the function with its definitions so that the linker will understand where to look up for the function definition in the libraries, regardless it is static or dynamic. 

In Windows, this file extension will be “.exe”, but in Linux, this file will be renamed “a.out”.


To view or add a comment, sign in

More articles by Yuan Fang

  • Dynamic library vs static library in C

    Couple weeks ago, I posted an article about the static library in C. As I learnt, there is another type of library come…

  • What is a C static library?

    This is my 3rd technical article at Holberton school. Further to the process of compiling a C program, this article…

  • Hard links v.s Soft inks

    This is the 2nd week of my BACK TO TECH journey. The topic I’m recently working on is SHELL.

    3 Comments

Others also viewed

Explore content categories