Compilation in “C” language with gcc

  1. Preprocessor
  2. Compilation
  3. Assembly
  4. Linker
  • PREPROCESSING:

Header Files

Macros

Conditional Compilation

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

Extract Preprocessed Code From GCC

C-Code// Header File
#include

#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;
} LC_ID,*LPLC_ID;

.......
.......
.......
# 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"
.text
.def printf; .scl 3; .type 32; .endef
.seh_proc printf
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
.seh_endprologue
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
ret
.seh_endproc
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.LC0:
.ascii "Hello World\0"
.LC1:
.ascii "%d\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $32, %rsp
.seh_stackalloc 32
.seh_endprologue
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
ret
.seh_endproc
.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
htd.c#include 
#include "htd.h"
void printHTD()
{
printf("Hack The Developer");
}
htd.hvoid printHTD();
cprogram.c#include "htd.h"
int main()
{
printHTD();
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 — https://nerdyelectronics.com/

Tip:

Picture from anime One Piece

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Best Gantt Chart Project Management Apps in 2020 (Free & Paid)

Integrating CarrierWave for File Upload in a Rails App

Cloud CO2 Footprint Calculators — Part 2: Azure Emissions Impact Dashboard

Print n Longest Lines

How to Send HTML Email Using Free Tools

The React Way Or The Highway!

Get More Better With Laravel Development Tools Developers Must Apply In 2021–22

{UPDATE} DeadRiot -- Zombie Shooter.

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
Smith Flores

Smith Flores

More from Medium

Top 5 Artificial Intelligence applications in Health Care

Lok Sewa Aayoge Computer Operator Microsoft Excel Model Set 23

Lok Sewa Aayoge Computer Operator Microsoft Excel  Model Set 23

CS371p Spring 2022 Week 2: Alejandro Balderas

Qobuz Vs Tidal Review: Who’s Better In 2022 — Centsr.com