0% found this document useful (0 votes)
100 views5 pages

Marvellous Infosystems - Pre-Placement Activity - Build Process of C Program Using GCC Toolchain

The document describes the 5 stages of the C program build process using the GCC toolchain: 1. Preprocessing - The preprocessor expands macros and includes header files. It outputs a preprocessed file. 2. Compilation - The compiler converts the preprocessed code into assembly language. It outputs an assembly file. 3. Assembly - The assembler converts the assembly code into machine instructions and generates an object file. 4. Linking - The linker combines object files and system libraries to create the final executable. 5. Loading - The loader loads the executable into memory so it can be executed by the operating system.

Uploaded by

ATHARVA PARDESHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views5 pages

Marvellous Infosystems - Pre-Placement Activity - Build Process of C Program Using GCC Toolchain

The document describes the 5 stages of the C program build process using the GCC toolchain: 1. Preprocessing - The preprocessor expands macros and includes header files. It outputs a preprocessed file. 2. Compilation - The compiler converts the preprocessed code into assembly language. It outputs an assembly file. 3. Assembly - The assembler converts the assembly code into machine instructions and generates an object file. 4. Linking - The linker combines object files and system libraries to create the final executable. 5. Loading - The loader loads the executable into memory so it can be executed by the operating system.

Uploaded by

ATHARVA PARDESHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Marvellous Infosystems : Pre-Placement Activity !

Build process of C Program using GCC toolchain

By using any editor (gedit, kwrite) write below C program that we want to refer to understand
different phases of toolchain

1.Preprocessor
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.
Preprocessor performs following tasks as
• File inclusion
• Macro expansion
• Conditional compilation
• Comment removal
• Extra Whitespace removal
For example, the #include <stdio.h> command in line 1 of Marvellous.c tells the preprocessor to
read the contents of the system header file stdio.h and insert it directly into the program text.

Piyush Khairnar - 7588945488 आम्ही Technical संस्कार करतो !!! ©Marvellous Infosystems Page 1
Marvellous Infosystems : Pre-Placement Activity !

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 Marvellous.c > Marvellous.i

The result is a file Marvellous.i that contains the source code with all macros expanded.
If we execute the above command in isolation then the file Marvellous.i will be saved to disk and
we can see its content by any editor.

Piyush Khairnar - 7588945488 आम्ही Technical संस्कार करतो !!! ©Marvellous Infosystems Page 2
Marvellous Infosystems : Pre-Placement Activity !

2. Compiler
In this phase compilation proper takes place.
Compiler converts human understandable code to Machine dependent code ie inn assembly
language.
The compiler translates Marvellous.i into Marvellous.s. File Marvellous.s contains assembly code.
We can explicitly tell gcc to translate Marvellous.i to Marvellous.s by executing the following
command.

[root@host ~]# gcc -S Marvellous.s

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

3. Assembler
Here, the assembler (as) translates Marvellous.s into machine language instructions, and
generates an object file Marvellous.o.
We can invoke the assembler at our own by executing the following command.
[root@host ~]# as Marvellous.s -o Marvellous.o

The above command will generate Marvellous.o as it is specified with -o option.

Piyush Khairnar - 7588945488 आम्ही Technical संस्कार करतो !!! ©Marvellous Infosystems Page 3
Marvellous Infosystems : Pre-Placement Activity !

And, the resulting file contains the machine instructions for the our program, with an undefined
reference to printf.

4. Linker
This is the final stage in compilation of our program.
This phase links object files to produce final executable file.
Linker links our .o file with other .o files.
An executable file requires many external resources (system functions, C run-time libraries etc.).
Regarding our program we have noticed that it calls the printf function to print the ‘Marvellous
Infosystems by Piyush Khairnar' message on console.
This function is contained in a separate pre compiled object file printf.o, which must somehow be
merged with our Marvellous.o file.
The linker (ld) performs this task for we.
Eventually, the resulting file Marvellous 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.
[root@host ~]# ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib64/crt1.o /
usr/lib64/crti.o /usr/lib64/crtn.o Marvellous.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 Marvellous

Piyush Khairnar - 7588945488 आम्ही Technical संस्कार करतो !!! ©Marvellous Infosystems Page 4
Marvellous Infosystems : Pre-Placement Activity !

5. Loader :
As above executable file is in hard disk but for execution purpose it should be loaded in RAM.
Loader is an Operating Dependent entity which loads our executable file from Hard disk into
Ram.
Belo command invokes the loader.
[root@host ~]# ./Marvellous

Output:
Marvellous Infosystems by Piyush Khairnar

For us, 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 Marvellous.c -o Marvellous

To get the intermediate files which are created in build process by using -save-temps option.

Piyush Khairnar - 7588945488 आम्ही Technical संस्कार करतो !!! ©Marvellous Infosystems Page 5

You might also like