Four GCC compilation processes

Source: Internet
Author: User

1. in Linux, we first compile a helloworld program for GCC compilation.

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

We can directly use gcc-O helloworld. C to compile the source file. c into an executable file helloworld.

However, the GCC compilation process can be divided into four steps:


2. Analysis of GCC commands-four steps

From the above we know that the GCC compilation source code generates the final executable binary program, and the GCC background implicitly executes four stages.


There are four steps for GCC to compile C source code: preprocessing -----> compilation ----> Link


Now we will use the GCC Command Options to analyze the GCC process one by one.
1) Pre-processing)
At this stage, the compiler compiles the header files in C source code, such as stdio. H. You can use the GCC option "-e" to view them.
Usage: # gcc-e hello. C-O hello. I
Purpose: output the hello. c preprocessing file hello. I.
[Root] # gcc-e hello. C-O hello. I
[Root] # ls
Hello. c hello. I



2) Compiling)
The second step is the compilation phase. In this phase, GCC should first check the code standardization and whether there are syntax errors to determine the actual work to be done by the Code, after checking that the code is correct, GCC translates the code into an assembly language. You can use the "-s" option to view the Code. This option is only compiled but not compiled to generate the assembly code.
Option-S
Usage: [root] # Gcc-s hello. I-O hello. s
Purpose: Compile the pre-processing output file hello. I into a hello. s file.
[[Email protected] hello-GCC] # ls
Hello. c hello. I hello. s



3) assembly phase)
In the Assembly phase, the ". s" file generated in the compilation phase is converted to the binary target code.
Option-C
Usage: [root] # gcc-C hello. S-O hello. o
Purpose: Compile the output file test. s and output the file test. O.
[Root] # Gcc-C hello. S-O hello. o
[Root] # ls

Hello. c hello. I hello. O hello. s


4) link stage)
After successful compilation, it enters the link stage.
No option Link
Usage: [root] # GCC hello. O-o hello.exe
Purpose: link the compiled output file hello.oto the final Executable File hello.exe.
[Root] # ls
Hello. c hello.exe hello. I hello. O hello. s

Run the executable file. The correct result is as follows.


[[Email protected] GCC] #./Hello

Hello world!





This article is written by cout_sev.

Some documents are taken from the Internet. If any errors occur, please correct them.

Reprinted please indicate the source: http://blog.csdn.net/cout_sev/

Thank you for your cooperation!


Four GCC compilation processes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.