- Published on
GCC Compilation Process
- Authors
- Name
- MrPuppeteer
- @mrpppteer
GCC compiles a C/C++ program into executable through a sequence of four steps, as depicted in the diagram above. To illustrate, let's consider the command g++ -o hello hello.cpp
1 and break down its execution:
Pre-processing: This initial step involves the GNU C Preprocessor (
cpp
), which handles tasks such as including headers (#include
) and expanding macros (#define
).cpp hello.cpp > hello.i
The resultant intermediate file
hello.i
contains the expanded source code.Compilation: The compiler takes the the pre-processed source code and translates it into assembly code specific to the target processor.
g++ -S hello.i
Using the
-S
flag instrructs the compiler to generate assembly code, instead of object code. The resultant assembly file ishello.s
.Assembly: The assembler (
as
) converts the assembly code into machine code, stored in an object file namedhello.o
.as -o hello.o hello.s
Linking: Finally, the linker (
ld
) combines the object code with any required library code to produce the executable filehello
.ld -o hello hello.o ...libraries...
If you're still unsure how to link object files using
ld
, you can run the compilation command with verbose mode enabled to get a detailed view of the process as shown below. Additionaly, you can refer to this stackoverflow post for further insights.
-v
)
Verbose Mode (To observe a detailed of the compilation process, you can enable the verbose mode by adding the -v
option along with the compilation command. For instance:
g++ -v -o hello hello.cpp
Resources
- https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
- https://askubuntu.com/questions/156392/what-is-the-equivalent-of-an-exe-file
- https://stackoverflow.com/questions/14163208/how-to-link-c-object-files-with-ld
Footnotes
It's worth noting that unlike Windows, which typically relies on file extensions like
.exe
to denote directly executable files, Linux/Unix systems often do not use file extensions for this purpose. Instead, they determine the executable type by examining the file itself. ↩