GCC command in Linux-parsing the four processes of GCC Compilation

Source: Internet
Author: User

To program C in Linux, you must use GNU gcc to compile C source code to generate executable programs. The GCC command is generally in the following format:

GCC [Option] file to be compiled [Option] [target file]. The target file can be the default. The executable file name generated by GCC is: Compile file. Out.
Let's take a look at the Classic Entry Program "Hello world! "
# Edit VI hello. C as follows:

# Include <stdlib. h>
# Include <stdio. h>
Void main (void)
{
Printf ("Hello world! /R/N ");
}

Compile it into an executable program using gcc. # GCC hello. C. This command directly generates the final binary executable program A. Out .. /A. out can be executed. Note: The main function must be available.
This command implicitly executes (1) preprocessing, (2) Assembly, (3) compilation, and (4) Links to form the final binary executable program. 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
-O client.exe client. C ). The pre-processing process information is displayed through VI hello. I.
2) Compile phase (Compiling ). 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 the check is correct, GCC translates the code into an assembly language. You can use the "-s" option to view and generate assembly code.
Usage: [root] # gcc-s hello. I-O hello. s
3) compilation phase ). In the Assembly phase, the ". s" file generated in the compilation phase is converted to the binary target code. Usage: [root] # gcc-C hello. S-O hello. o
4) link stage ). Usage: [root] #
GCC hello. O-o hello.exe: links the compiled output file hello.oto the final Executable File hello.exe.
The "printf" function implementation is not defined in this program, and the "stdio. h "is only the declaration of the function, but does not define the implementation of the function. So where does it implement the" printf "function? The final answer is: the system has implemented these functions in the library file named libc. so.6. You can run the LDD command to view the dynamic library loading status: [root] # LDD hello.exe.

Function libraries are generally divided into static libraries and dynamic libraries. A static library is used to add all the code of the library file to the executable file when compiling the link. Therefore, the generated file is large, but the library file is no longer needed at runtime. The suffix is generally ". ". On the contrary, the dynamic library does not add the code of the library file to the executable file during the compilation link, but loads the library from the link file during the program execution, this saves the system overhead. The dynamic library is generally suffixed with ". So". As mentioned above, libc. so.6 is the dynamic library. By default, dynamic libraries are used for GCC compilation.

 

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.