Compilation in “C” language with gcc

  1. Preprocessor
  2. Compilation
  3. Assembly
  4. Linker

Header Files

The source file consists of a number of header files in the C language, and it is the preprocessor’s task to include the library.


Macros are defined by #define syntax in the C programming language. During the preprocessing stage, the macros are replaced by their values.

Conditional Compilation

Often we want our compiled code to be minimal, so we can use conditional compilation. The preprocessor often operates on a conditional compilation and reduces the code by adding only those lines that fulfill the condition.

  • #undef
  • #ifdef
  • #ifndef
  • #if
  • #else
  • #elif
  • #endif

Extract Preprocessed Code From GCC

We can also take a look at the file from each part of the compilation pipeline. Let’s ask the C compiler driver to dump the translation unit without going any further.

C-Code// Header File

#define Max 10

int main()
printf("Hello World"); // Print Hello World
printf("%d", Max);
return 0;
Dump Translation Unit$ gcc -E cprogram.c

# 1 "cprogram.c"
# 1 ""
# 1 ""
# 1 "cprogram.c"
typedef __time64_t time_t;
# 435 "C:/msys64/mingw64/x86_64-w64-mingw32/include/corecrt.h" 3

typedef struct localeinfo_struct {
pthreadlocinfo locinfo;
pthreadmbcinfo mbcinfo;
} _locale_tstruct,*_locale_t;

typedef struct tagLC_ID {
unsigned short wLanguage;
unsigned short wCountry;
unsigned short wCodePage;

# 1582 "C:/msys64/mingw64/x86_64-w64-mingw32/include/stdio.h" 2 3
# 3 "cprogram.c" 2

# 5 "cprogram.c"
int main()
printf("Hello World");
printf("%d", 10);
return 0;
Extract Assembly Code from GCC$ gcc -S cprogram.c

$ cat cprogram.s

.file "cprogram.c"
.def printf; .scl 3; .type 32; .endef
.seh_proc printf
pushq %rbp
.seh_pushreg %rbp
pushq %rbx
.seh_pushreg %rbx
subq $56, %rsp
.seh_stackalloc 56
leaq 128(%rsp), %rbp
.seh_setframe %rbp, 128
movq %rcx, -48(%rbp)
movq %rdx, -40(%rbp)
movq %r8, -32(%rbp)
movq %r9, -24(%rbp)
leaq -40(%rbp), %rax
movq %rax, -96(%rbp)
movq -96(%rbp), %rbx
movl $1, %ecx
movq __imp___acrt_iob_func(%rip), %rax
call *%rax
movq %rbx, %r8
movq -48(%rbp), %rdx
movq %rax, %rcx
call __mingw_vfprintf
movl %eax, -84(%rbp)
movl -84(%rbp), %eax
addq $56, %rsp
popq %rbx
popq %rbp
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.ascii "Hello World\0"
.ascii "%d\0"
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $32, %rsp
.seh_stackalloc 32
call __main
leaq .LC0(%rip), %rcx
call printf
movl $10, %edx
leaq .LC1(%rip), %rcx
call printf
movl $0, %eax
addq $32, %rsp
popq %rbp
.ident "GCC: (Rev3, Built by MSYS2 project) 10.2.0"
.def __mingw_vfprintf; .scl 2; .type 32; .endef
  • Create object from the assbembly file:
create object file from assembly file$ as cprogram.s -o cprogram.o
create object file$ gcc -c cprogram.c
#include "htd.h"
void printHTD()
printf("Hack The Developer");
htd.hvoid printHTD();
cprogram.c#include "htd.h"
int main()
return 0;
$ gcc -c cprogram.c$ gcc -c htd.c
$ ld htd.o cprogram.o -o cprogram.exeC:\msys64\mingw64\bin\ld.exe: cprogram.o:cprogram.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
C:\msys64\mingw64\bin\ld.exe: cprogram.o:cprogram.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
C:\msys64\mingw64\bin\ld.exe: cprogram.o:cprogram.c:(.text+0x78): undefined reference to `__main'
$ gcc htd.o cprogram.o -o cprogram.exe$ ./cprogram.exeHack The Developer
source —


Don’t worry if you don’t understand much about programming, we were all newbies once, don’t get frustrated, keep going, be patient and persevere in your dreams!

Picture from anime One Piece



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store