GCC compiles a C/C++ program into executable in 4 steps as shown in the diagram below. For example a: "gcc -o hello.exe hello.c" is carried out as follows:
Lets understand this with the a simple example.
#include <stdio.h>
int main(){
printf("This is an example of compilation process using gcc");
return 0;
}
1. Preprocessing: via the GNU C Preprocessor(cpp.exe) which includes the headers(#include) and expands the macros(#define). The command is cpp hello.exe > hello.i . Note here that the command has cpp not gcc as we are using cpp.exe which is the C preprocessor.
The resultant intermediate file "hello.i" contains the expanded source code. Click on the link below to see how the file looks like.
http://pastebin.com/YBr6yt1s
2. Compilation: The compiler compiles the pre-processed source code into an assembly code for a specified processor.
The command is : gcc -S hello.i
The -S option specifies the pre-processed source code, instead of object code.The resultant assembly is "hello.s".
3. Assembly : The assembler (as.exe) converts assembly code into machine code.
The command is : as -o hello.o hello.s
4. Linker: Finally the linker links the object code with the library code to produce an executable file.
ld -o hello.exe hello.o ...libraries...
We can see the detailed compilation process by enabling -v(verbose) option.
gcc -v hello.c -o hello.exe
Lets understand this with the a simple example.
#include <stdio.h>
int main(){
printf("This is an example of compilation process using gcc");
return 0;
}
1. Preprocessing: via the GNU C Preprocessor(cpp.exe) which includes the headers(#include) and expands the macros(#define). The command is cpp hello.exe > hello.i . Note here that the command has cpp not gcc as we are using cpp.exe which is the C preprocessor.
The resultant intermediate file "hello.i" contains the expanded source code. Click on the link below to see how the file looks like.
http://pastebin.com/YBr6yt1s
2. Compilation: The compiler compiles the pre-processed source code into an assembly code for a specified processor.
The command is : gcc -S hello.i
The -S option specifies the pre-processed source code, instead of object code.The resultant assembly is "hello.s".
3. Assembly : The assembler (as.exe) converts assembly code into machine code.
The command is : as -o hello.o hello.s
4. Linker: Finally the linker links the object code with the library code to produce an executable file.
ld -o hello.exe hello.o ...libraries...
We can see the detailed compilation process by enabling -v(verbose) option.
gcc -v hello.c -o hello.exe
No comments:
Post a Comment