Compilation in “C” language with gcc

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

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

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
#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:

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