g++ -Wall -o sample sample.cpp
command (file name is sample.cpp) to compile the program then we run the executable output from that process.In this this post lets see how this compilation process happen before output an executable.
This compilation process has four main stages.
Preprocessing
In this stage preprocessor copies the contents of the included header files into the source code file, generates macro code, and replaces symbolic constants defined using #define with their values.
Normally the standard header files to be included are in "/usr/include" directory in Ubuntu. [GCC looks in several different places for headers]
We can stop the compilation process after preprocessing usingg++ -E sample.cpp
Output we get after this stage might be very long because all the header files are also included.
So directing the output to a txt file would be better.Compiling
In this stage the expanded source code file produced by the C++ preprocessor is compiled into the assembly language for the platform.
We can stop the process after this stage using-S
opyion.g++ -Wall -S sample.cpp
As a result of above command, a file named sample.s will be created.Assembling
In this stage the assembler code generated by the compiler is assembled into into machine code producing actual binary file in some format(ELF, COFF, a.out, ...).
To stop the process after the assembly step, we can use the -c option
g++ -Wall -c sample.cpp
As a result of this command an object file named sample.o will be created. Normally we cant read the content of this object file using a text editor. It can be read using objdump command through a terminal.
assembler contents of executable sections of an object file can be displayed using -d option of objdump command
objdump -d sample.o
[Many other objdump options can be found here.]
Linking
In this stage the object code file generated by the assembler is linked together with the object code files(.o , .a) for any library functions used to produce an executable file. After the linking final executable will be returned. Normally these object code files for other libraries are in "/usr/lib/" directory in Ubuntu.
g++ -E sample.cpp > AfterPrePros.txt
NOTE: 1. Static library - In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.
No comments:
Post a Comment