C Build Process
by
[Link]
Overview of Build process:
GNU toolchain
The GNU toolchain is a broad collection of programming tools produced by the GNU Project.
Major components of GNU toolchain are:
•GNU make: an automation tool for compilation and build
•GNU Compiler Collection (GCC): a suite of compilers for several programming languages
•GNU C Library (glibc): core C library including headers, libraries, and dynamic loader
•GNU Binutils: a suite of tools including linker, assembler and other tools
•GNU m4: an m4 macro processor
•GNU Debugger (GDB): a code debugging tool
GCC(GNU Compiler Collection):
What is compiler ?
A compiler is system software which converts programming language code (human-readable source code)into binary format(machine-readable
code or an intermediate code)in single steps.
GCC Compiler:
● GCC stands for GNU Compiler Collection . It is an integrated distribution of compilers, written in C language and
developed by GNU Project, that supports a lot of programming languages: C, C++, Objective-C, Fortran, Go and
more.
● The process of transforming source code into an executable involves the following stages: preprocessing,
compilation, assembly, and linking.
● In Linux, there is a command called gcc that generates an executable file from a .c file
Compile with GCC:
● gcc hello.c -o hello
● This command tells GCC to compile the hello.c source code and create an executable named hello (-o option
specifies the output file).
Run the Executable:
● After the compilation is successful, run the executable:
./hello
1. Preprocessing:
gcc -E main.c > preprocessor
Description:
● The -E option tells GCC to stop after preprocessing.
● Preprocessor remove comments and include header files in source code, replace macro with code.
● #include <stdio.h> // Gets replaced by actual contents
2. Compilation
The compiler takes the output file of the preprocessor and generates an assembly code. In Linux, the command line
that allows to generate that file is gcc with the -S option:
Description:
● The -S option instructs GCC to stop after compilation (generating assembly code).
3. Assemble
This component of GCC takes the assembly code, and converts it into a machine code(object code). It means, this is an
object file with low-level language.
● In this step, the output file has an .o extension. If this file is open, it could be seen the characters understood by
the machine.
● When compiling a C program, an object file (.o) is an intermediate file that contains compiled machine code but is not yet executable.
4. Linking
The linker merges the codes into a single one. Also, this component of GCC merges library function codes declared with
the codes developed.
In this step, the executable is already generated (main).
There is another option to obtain all of the files generated in each step of the compilation process, which is the
following (using the -save-temps option):
● -Wall: This option stands for "enable all warnings." It instructs the compiler to generate
warning messages for a wide range of potential issues in the source code. Using -Wall is
a good practice to catch common programming errors.
● -save-temps: This option tells the compiler to keep intermediate files generated during
the compilation process.
Commands:
Preprocessing: gcc -E hello.c > hello.i -> hello.i created
Code generator/compiler: gcc -S hello.i -> hello.s created
Assembler: gcc -c hello.s -> hello.o created
Linker: gcc -Wall -save-temps hello.o -o hello -> hello created
Components of a C Program
Explanation:
1. scanf("%s", name); (No &)
○ name is declared as a character array (char name[20]).
○ In C, the name of an array itself acts as a pointer to the first element of the array.
○ scanf("%s", name); already receives the memory address of the first character of name, so no & is needed.
2. scanf("%d", &age); (Uses &)
○ age is an integer variable (int age).
○ scanf("%d", &age); needs the memory address of age so that it can store the input value at that location.
○ Since age is not an array, using &age ensures we pass its address to scanf.
General Rule:
● For arrays (like char name[]), do NOT use & because the array name already acts as a pointer.
● For normal variables (like int age), use & to pass the memory address.