echo '' ;
C Programming Language

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

Reference

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

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

Continue reading