C Compiling process

In programming, to understand the complete flow of software generation, you need to very clear in compiling process steps. 5 Stages of the compiling process (refer to the below Image)

  1. Pr-processing.
  2. Compiler.
  3. Assembler,
  4. Linker,
  5. Executable file.


Here is the example of hello world program to understand the entire compiling process. Usually, we will use some IDE like Code Block or Dev C++ for our c program to compile and generate the executable file. In my case, I used the portable IDE.

Run GCC without installation on the Windows Platform

If you have a question like how can I run the GCC without install in windows? Here your solution

  • Download the Dev C ++ portable version from the internet. ( I have downloaded the portable version Dev-Cpp 5.7.1 MinGW 4.8.1 Portable.7z)
  • In my case, I have kept the potable version directories (Folders) in this location D:\ArunEworld\@ Software\Dev C++\Portable\
  • Refer to the below hello world example code and save it as HelloWorld.c in the directory D:\ArunEworld\@ Software\Dev C++\Portable\Dev-Cpp 5.7.1 MinGW 4.8.1 Portable\Dev-Cpp\MinGW32\bin

Code: HelloWord.c

#include <stdio.h>
int main()
{
    printf("ArunEworld");
    return 0;
}

Compile and Run using GCC in the command prompt

ActionSteps
Open the command prompt in GCC.exe directoryYou can do this in two ways.

The first option is to click WINDOWS + R, enter the text cmd and click ENTER, Command prompt will be open, then use the CD command to change the directory to D:\ArunEworld\@ Software\Dev C++\Portable\Dev-Cpp 5.7.1 MinGW 4.8.1 Portable\Dev-Cpp\MinGW32\bin

The second option is to go to the directory D:\ArunEworld\@ Software\Dev C++\Portable\Dev-Cpp 5.7.1 MinGW 4.8.1 Portable\Dev-Cpp\MinGW32\bin using windows explore and Click the PATH area and type cmd the text then click ENETR.
GCC commands--help – for display the help
--version Display compiler version information
-E Preprocess only; do not compile, assemble or link
-S Compile only; do not assemble or link
-c Compile and assemble, but do not link
-o Place the output into
Compile HelloWorld.c using GCCIn command, Prompt type the following and compile the code. (Observe the new HelloWorld.exe file generation while compiling time in the same directory)

>gcc.exe HelloWoldExample.c -o HelloWoldExample

Run the Executable file>HelloWorld.exe

Output will be ArunEworld

Hope you learned how to compile the c program in GCC and run the GCC without install in Windows. Now we are going to learn the complete compiling process.

Pre-Processor

Preprocessing stage of the HelloWorld.c using gcc in Windows

-E Compile only; do not assemble or link.

Run : gcc.exe -E HelloWorld.c

Output:

# 1 "HelloWold.c"
# 1 "<command-line>"
# 1 "HelloWold.c"

[lines omitted for brevity]

extern int __vsnprintf_chk (char * restrict, size_t,
       int, size_t, const char * restrict, va_list);
# 28 "d:\\ArunEworld\\@ software\\dev c++\\portable\\dev-cpp 5.7.1 mingw 4.8.1 portable\\dev-cpp\\mingw32\\include\\stdio.h" 2 3
# 2 "HelloWorld.c" 2
void main(void) {
 printf("ArunEworld");

}

Example-2

This code is for just only macro.

#define WEBSITE "www.aruneworld.com"
int main()
{
	WEBSITE;
    return 0;
}
  • gcc -E ArunEworld.c
# 1 "test.c"
# 1 "<command-line>"
# 1 "test.c"

int main()
{
 "www.aruneworld.com";
    return 0;
}

Compiler

Syntax Verification & Convert C program into Assembly Code.

Compiling stage of the HelloWorld.c using gcc in Windows

-S Compile only; do not assemble or link

Run : gcc.exe -S HelloWorld.c

Output: HelloWorld.s

.file	"HelloWold.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
LC0:
	.ascii "ArunEworld\0"
	.text
	.globl	_main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
LFB6:
	.cfi_startproc
	pushl	%ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	movl	%esp, %ebp
	.cfi_def_cfa_register 5
	andl	$-16, %esp
	subl	$16, %esp
	call	___main
	movl	$LC0, (%esp)
	call	_printf
	leave
	.cfi_restore 5
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
LFE6:
	.ident	"GCC: (GNU) 4.8.1"
	.def	_printf;	.scl	2;	.type	32;	.endef

Assembler

Assembly language into Machine code(0s and 1s)

Compiling stage of the HelloWorld.c using gcc in windows

-C Compile and assemble, but do not link

Run : gcc.exe -c HelloWorld.c

Output: HelloWold.o

Linker & Executable

Link all Library Files and generate an exe file. In Windows -o command will two both process.

Linking & Generate .exe stage of the HelloWorld.c using gcc in Windows

-o Place the output into.

Run : gcc.exe HelloWorld.c -o HelloWorld

Output: HelloWold.exe

If i dont have any obj file then what is the action of linker?

Answere

Because the linker expects at least one object file containing some machine code to produce an executable.

If you invoke the linker directly without any object files:

  • There’s nothing to link.
  • The linker will report an error like:
    • ld: no input files
    • LINK : fatal error LNK1561: entry point must be defined (Visual Studio)
    • or a similar message depending on your toolchain.

Sections in a CMD file

In a typical linker command file (.cmd), you can see sections such as:

  • .text
  • .cinit
  • .const
  • .econst
  • .pinit
  • .switch

And also sections like:

  • .bss
  • .ebss
  • .stack
  • .sysmem
  • .esysmen

These are larger and more granular than generic sections like TEXT, DATA, BSS (common in ARM systems), heap, and stack.


Definition of TI segments:

Summary:
The TI linker command file breaks program memory into multiple specific segments:

  • Initialized sections for code and preloaded data.
  • Uninitialized sections for variables and runtime needs like stack and heap.

This is more fine-grained than the traditional TEXT, DATA, BSS layout and matches the memory model and feature set of TI embedded toolchains.

Initialized segments

SectionDescription
.textHolds all executable code and constants
.cinitC initialization records for global and static variables
.constInitialization and description of string constants, global and static variables declared with const
.econstInitialization and description of string constants and global/static variables declared as far const
.pinitList of global C++ constructors
.switchTables for switch statement jump addresses

Uninitialized segments

SectionDescription
.bssSpace reserved for uninitialized global and local variables. Initialized at startup by copying data from .cinit.
.ebssSpace for uninitialized global and static variables when large data model is used. Initialized similarly at startup.
.stackSpace for the system stack — used for function calls and large local variables.
.sysmemReserved space for dynamic allocation (malloc, etc.). Empty if no dynamic allocation is used.
.esysmenReserved space for dynamic allocation if using far functions or data. Empty if unused.

Memory Map Understand in C program multiple files?

Reference

What is cross compiler?

A cross compiler is mainly used in embedded systems or situations where the target device cannot compile its own code.

Here’s the idea:

StepHost SystemTarget System
1. Write codeOn your development PC (e.g., Windows/Linux)
2. Compile codeUsing a cross compiler that produces code for a different CPU architecture (e.g., ARM, AVR, MIPS)
3. Run codeOn the target hardware (e.g., microcontroller, IoT device)

Why use a cross compiler?

  • Target hardware may have limited RAM, storage, or processing power — can’t run a full compiler.
  • Development speed is much faster on a PC.
  • Multiple platforms can be supported from one development environment.

Example tools:

  • arm-none-eabi-gcc → Compiles C/C++ for ARM Cortex-M microcontrollers.
  • avr-gcc → Compiles code for AVR microcontrollers like Arduino.
Please turn AdBlock off, and continue learning

Notice for AdBlock users

Please turn AdBlock off
Index

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading