All the steps of compilation
steps of compilation in C language

All the steps of compilation

C is a compiled language. Its source code is written using any editor of a programmer’s choice in the form of a text file, then it has to be compiled into machine code.

Four Steps of Compilation: preprocessing, compiling, assembly, linking.

1. Preprocessing

During compilation of a C program the compilation is started off with preprocessing the directives (e.g., #include and #define). The preprocessor (cpp - c preprocessor) is a separate program in reality, but it is invoked automatically by the compiler. For example, the #include <stdio.h> command in line 1 of helloworld.c tells the preprocessor to read the contents of the system header file stdio.h and insert it directly into the program text. The result is another file typically with the .i suffix. In practice, the preprocessed file is not saved to disk unless the -save-temps option is used.

This is the first stage of compilation process where preprocessor directives (macros and header files are most common) are expanded. To perform this step gcc executes the following command internally.

[root@host ~]# cpp helloworld.c > helloworld.i

The result is a file helloworld.i that contains the source code with all macros expanded. If you execute the above command in isolation then the file helloworld.i will be saved to disk and you can see its content by vi or any other editor you have on your Linux box.


2. Compilation

In this phase compilation proper takes place. The compiler (ccl) translates helloworld.i into helloworld.s. File helloworld.s contains assembly code. You can explicitly tell gcc to translate helloworld.i to helloworld.s by executing the following command.

[root@host ~]# gcc -S helloworld.i

The command line option -S tells the compiler to convert the preprocessed code to assembly language without creating an object file. After having created helloworld.s you can see the content of this file. While looking at assembly code you may note that the assembly code contains a call to the external function printf.

3. Assembly

Here, the assembler (as) translates helloworld.s into machine language instructions, and generates an object file helloworld.o. You can invoke the assembler at your own by executing the following command.

[root@host ~]# as helloworld.s -o helloworld.o

The above command will generate helloworld.o as it is specified with -o option. And, the resulting file contains the machine instructions for the classic "Hello World!" program, with an undefined reference to printf.

4. Linking

This is the final stage in compilation of "Hello World!" program. This phase links object files to produce final executable file. An executable file requires many external resources (system functions, C run-time libraries etc.). Regarding our "Hello World!" program you have noticed that it calls the printf function to print the 'Hello World!' message on console. This function is contained in a separate pre compiled object file printf.o, which must somehow be merged with our helloworld.o file. The linker (ld) performs this task for you. Eventually, the resulting file helloworld is produced, which is an executable. This is now ready to be loaded into memory and executed by the system.

The actual link command executed by linker is rather complicated. But still, if you passionate enough you can execute the following command to produce the executable file helloworld by yourself.

[root@host ~]# ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib64/crt1.o /usr/lib64/crti.o /usr/lib64/crtn.o helloworld.o /usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtbegin.o -L /usr/lib/gcc/x86_64-redhat-linux/4.1.2/ -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc/x86_64-redhat-linux/4.1.2/crtend.o -o helloworld

And, you can greet the universe as follows:

[root@host ~]# ./helloworld

Output:

hello, world!

I executed the above command on an x86_64 system having gcc 4.1.2. It may be the above command does not work on your system as it is. It all matters that where the libraries located?

For you, there is no need to type the complex ld command directly - the entire linking process is handled transparently by gcc when invoked, as follows.

[root@host ~]# gcc helloworld.c -o helloworld


During the whole compilation process there are other files also in role along with the source code file. If you see the very first statement of helloworld.c it is #include <stdio.h> (includes header file). Likewise, while compiling a C program you have to work with following types of files.


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

ahmed zghal的更多文章

  • Welcome To Staffy Code Base

    Welcome To Staffy Code Base

    Empowering Your Workforce "Welcome to the future of workplace management with Staffy, the cutting-edge mobile…

    1 条评论
  • What is like being a Designer (UI/UX)

    What is like being a Designer (UI/UX)

    Overview Spotify is a digital music service that gives you access to millions of songs, podcasts, and videos from…

  • Portfolio Project: TunisianDoom

    Portfolio Project: TunisianDoom

    Introduction: this is a simple game made by me Mohamed Ahmed Zghal, it is a game meant to make you have fun and explore…

  • What happens when you type google.com in your browser and press Enter

    What happens when you type google.com in your browser and press Enter

    Nowadays in the digital era, we use the internet for just about everything. We can study, connect with our friends and…

  • Internet of Things (IoT)

    Internet of Things (IoT)

    What is IOT ? The internet of things, or IoT, is a system of interrelated computing devices, mechanical and digital…

  • The concept of Recursion

    The concept of Recursion

    Recursion, what is it and why use it? ? Recursion is a programming technique using a function that calls itself one or…

  • Python3: Mutable, Immutable... everything is object!

    Python3: Mutable, Immutable... everything is object!

    1 >>> Introduction In object-oriented programming languages like Python, an object is an entity that contains data…

  • The differences between static and dynamic libraries

    The differences between static and dynamic libraries

    Why use libraries? As mentioned before, libraries allow us to store function definitions, aka pieces of code that will…

  • What happens when you type `ls -l *.c` in the shell

    What happens when you type `ls -l *.c` in the shell

    Let’s take a look at what happens when you type ls *.c in the shell.

  • Hard and Symbolic links on Linux

    Hard and Symbolic links on Linux

    What is Linux? Just like Windows, iOS, and Mac OS, Linux is an operating system. In fact, one of the most popular…

社区洞察

其他会员也浏览了