Linux GCC compilation process

Source: Internet
Author: User

The GCC compilation process is divided into four steps:
(1) pre-compilation (pre-processing)
(2) Compile (Compiling)
(3) Assembly)
(4) Link (linking)
Taking hello. C as an example:
# Include <stdio. h>
Int main (void)
{
Printf ("Hello world! ");
Return 0;
}

(1) pre-compilation phase (pre-processing)
At this stage, the compilerCodeStdio. h In, and use gcc-E for viewing (-e enables GCC to stop compilation after preprocessing)
Gcc-e hello. C-O hello. I
Check hello. I. It can be seen that GCC does perform preprocessing. It inserts the content of "stdio. H" into the hello. I file.
(2) compilation phase
At this stage, GCC first checks the code standardization and whether there are syntax errors to determine the actual work to be done by the Code.
GCC compiles the code into an assembly language. You can use the-s option to view the Code. This option is used for compilation instead of compilation to generate the assembly code.
Gcc-s hello. I-O hello. s
(3) Assembly stage
It is to convert the. s file generated in the compilation phase to the target file. You can use option-C to convert the assembly code to the. O binary target code.
Gcc-C hello. S-O hello. o

(4) link stage
After successful compilation, it enters the link stage, where an important concept is involved: the function library.
The reader re-views this smallProgramThe implementation of the function "printf" is not defined in the applet, and the "stdio. H" is included in the pre-compilation"
In addition, only the declaration of the function is required, but the implementation of the function is not defined. Where can I implement the "printf" function ?, The final answer is: The system implements these functions.
All of them are saved in the library file named libc. so.6. If not specified, GCC will go to the default search path "/usr/lib" of the system to search for them.
That is, link to the libc. so.6 library function to implement "printf", which is also the role of the link.
The function library is generally divided into two types: static library and dynamic library. Static library refers to adding all the code of the library file to the executable program during the compilation link, so the generated
The executable file is large, and the library file is no longer needed at runtime. The suffix is.. In contrast to dynamic libraries, the library file code is not compiled during the link.
Add to the executable file, but load the library by the link file during program execution, which can save the system overhead. The dynamic library is generally suffixed with. So.
After the link is complete, GCC can generate an executable file.

GCC hello. O-O hello

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.