The compilation process of LinuxGCC-general Linux technology-Linux programming and kernel information. The following is a detailed description. Currently, the most common C language Compiler in Linux is GCC (GNU Compiler Collection), which is a compilation system that complies with the ansi c standard in the GNU project, compile programs written in C, C ++, Object C, and other languages. not only is GCC very powerful, but its structure is also extremely flexible. one of the most commendable points is that it can support a variety of 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. It is one of the representative works of GNU. Gcc is a super compiler that can compile executable programs on multiple hardware platforms. Its execution efficiency is 20% higher than the average efficiency of general compilers ~ 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 process, it analyzes the file inclusion (include) and pre-compiled statements (such as macro definition define) in the source code file. Next, call the "cc0" command to compile the file. At this stage, the target file with the. o suffix is generated based on the input file. The assembly process is a step for the assembly language and calls as for work. Generally ,. S is the suffix of the assembly language source code file and assembly ,. the assembly language files suffixed with s are generated after pre-compilation and assembly. o is the target file with the suffix. After all the target files are generated, gcc calls ld to complete the final key work. This stage is the connection. During the connection phase, all target files are arranged in the proper location of the executable program. At the same time, the library functions called by this program are also connected to appropriate places from their respective archives.
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 the content contained in the "# include" statement into it; then, GCC will call ccl and as to compile the processed source code into the target code. Finally, GCC will call the link program ld to link the generated target code into 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.
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