H_e_d
Compilation of C program
3 min readFeb 10, 2021

--

steps of gcc
steps of gcc

Compilation process in C

The C is an inspiring low-level language that is still useful for system programming or robotic and that brings the programmers closer to the execution of the program.

The GCC (GNU compiler collection) is a software capable of compiling fewer programming languages including C. It was included in most Unix/Linux platforms.

What is happening when a file is compiled ?

Steps of compilation:

Compiling a C file create an executable file to finally make the code usable.

This can be done using the gcc command onto your .c file.

gcc test.c

If a main function is found and no error are returned the file will compile. By default you should see using ls -l command an a.out executable file.

-rwxrwxr-x  1 hedy hedy 16464 févr. 10 17:36 a.out

To achieve this gcc has run through 4 main steps.

  • Preprocessing :

The preprocessor called macros preprocessor will transform your code before compilation. On top of replacing comments with spaces it will include headers (library) and search for macros and replace macros names by their expansions if there is.

Unless you give it directives (starting with #) the preprocessing will not do much changes.
You may find out the output of the preprocessing using the -E option of gcc.

gcc -E test.c

The output should look like this depending on your code:

# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "test.c"
int main(void)
{
return(0);
}
  • Compiling:

The compiling process can now read the output of the preprocessor and convert it into binary also called assembling language. You can still manage to see the file before the assembling using -S option to gcc.

gcc -S test.c

This will create a readable file with .s extension like so:

cat test.s 
.file "test.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0"
.section .note.GNU-stack,"",@progbits
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
.long 0xc0000002
.long 3f - 2f
2:
.long 0x3
3:
.align 8
4:
  • Assembling:

The assembling turns the file to a binary file also called “object file”. To obtain and read the object file you can use -c option to the gcc command. The file will compile but not link which is the next step.

gcc -c test.c

Your object file will take the .o extension.

  • Linking:

This is the final step of the compilation. All objects code will be merged to make the program executable. With no option to gcc the a.out executable will be create.

You can name your executable file by using the -o option to gcc like so:

gcc test.c -o test

This will create an executable file that you can execute like so:

./test

--

--