Linux GCC compilation process

Source: Internet
Author: User
Currently, the most common C language compiler in Linux is GCC (GNU Compiler Collection ).
The ansi c standard compilation system can compile programs written in C, C ++, Object C, and other languages. GCC is not only very functional
Powerful and flexible in structure. The most commendable aspect is that it supports various languages through different front-end modules.
Such as Java, Fortran, Pascal, Modula-3 and Ada.
In Linux, GCC (gnu c compiler) is a powerful and superior multi-platform compiler launched by GNU,
Is one of GNU's representative works. GCC is a super compiler that can compile executable programs on multiple hardware platforms.
The execution efficiency is 20% higher than the average compiler efficiency ~ 30%.
When you use GCC to compile a program, the compilation process can be divided into four stages:
◆ Pre-processing)
◆ Compile (Compiling)
◆ Assembly)
◆ Link (linking)
. C is a suffix file, C language source code file;
. A is a file with a suffix. It is a file library consisting of the target file;
. C,. cc or. cxx is a C ++ source code file;
. H is a suffix file, which is the header file included by the program;
. I is a file with a suffix. It is a pre-processed C source code file;
. II is a file with a suffix. It is a pre-processed C ++ source code file;
. M is a suffix file, which is an objective-C source code file;
The. o file is the compiled target file;
. S is a suffix file, which is an assembly language source code file;
. S is a precompiled assembly language source code file.
The command GCC first calls CPP for preprocessing. During the preprocessing
), Pre-compiled statements (such as macro definition define) for analysis. Then, call the command 'cc1' to compile the program.
File generation target file with. O as the suffix. The assembly process is a step for the assembly language and calls as for work.
In general,. S is the suffix of the assembly language source code files and assembly,. S is the suffix of the assembly language files after pre-compilation and
After compilation, the target file with the. O suffix is generated. After all target files are generated, GCC will call LD to complete
The final key task is connection. During the connection phase, all target files are scheduled to be executable.
The appropriate position in the program. At the same time, the library functions called by the program are also connected from their respective archives to the appropriate
.
Example 1: Hello. c
# Include
Int main (void)
{
Printf ("Hello world, Linux programming! /N ");
Return 0;
}
Run the following command to compile and run the program:
# GCC hello. C-O hello
#./Hello
Hello world, Linux programming!
First, GCC needs to call the Preprocessing Program CPP, which is responsible for expanding the macro defined in the source file and inserting "# In
The content of the clude statement. GCC then calls CCL and as to compile the processed source code into the target code;
Finally, GCC calls the link program LD to link the generated target code to an executable program.
◆ Pre-processing)
You can use the-e parameter to stop the GCC compilation process after preprocessing:
# Gcc-e hello. C-O hello. cpp
◆ Compile (Compiling)
The next step is to compile hello. I as the target code, which can be done by using the-C parameter:
# Gcc-x CPP-output hello. cpp-O hello. o
Or
# Gcc-C hello. cpp-O hello. o
◆ Assembly)
◆ Link (linking)
# GCC hello. O-O hello
The Assembly should call the AS. This should be done together.
Main GCC parameters:
-O defines the output file
-E: preprocessing/pre-compilation stops.
-C: only generate. O, not connected
-X indicates the compilation procedure.
-G gdb for debugging, including standard debugging information in executable programs
-O, O1, O2, O3, O4, O5 optimization level
-W disable all warnings
-Wall allows all useful warnings
-Dfoo = bar defines the value of the pre-processing macro Foo as bar
-Idirname: Add dirname to the search directory containing files.
-Ldirname: Add dirname to the search directory of the library file.
-Static Link Library
-Lfoo: dynamically connects to the library named libfoo. O/libfoo. So (2.4/2.6)
-Mcpu = the CPU type is optimized for different CPUs, such as-m386 and-mpentiumpro.

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.