GCC compilation process

Source: Internet
Author: User

The following is the general compiler procedure for C programs:

The GCC compilation process is divided into four steps, namely:
· Pretreatment (pre-processing)

The C language is preprocessed to generate the *.i file.
· Compiling (compiling)

Compile the *.i file generated in the previous step to build the assembly language file with the suffix named *.s
· Compilation (assembling)

Assemble the assembly language file *.s, generate the target file with the suffix named *.O
· Links (linking)

  Link the *.o files of each module to generate the final executable file

Example code:

1 // hello.c Source Code 2 3 #include <stdio.h>4int  main ()5{6     printf ("Hello world!\n"); 7     return 0 ; 8 }

1. Pre-programmed translation

gcc -e hello.c-o hello.i

The compiler processes various preprocessing directives (lines of code that start with # #include #define #ifdef, etc.), as well as deleting comments, adding line numbers and file name identifiers.

The preprocessor (CPP) modifies the original C program based on the command (directives) that begins with the character #. such as the # include <stdio.h> directive in hello.c tells the preprocessor to read the contents of the system header file stdio.h and insert it directly into the program text. The result is another C program, usually with. I as the file name extension.

2. Compiling

gcc -S Hello.i-o Hello.s

The compiling process is a series of lexical analysis, grammar analysis, semantic analysis and optimization of the pre-processed files to produce the corresponding assembly code files.

3. Compilation

gcc -C hello.s-o hello.o

The assembler process is a binary file that the compiler converts the assembly code to the computer-recognizable.

4. Links

GCC Hello.o-o Hello

In this source program does not define "printf" function implementation, and in the pre-compilation included in the "Stdio.h" also only the declaration of the function, but not the implementation of the definition function, Because these functions are implemented by the system is named Libc.so.6 Library file, when not specifically specified, GCC will go to the system default search path "/usr/lib" to find, that is, link to the libc.so.6 library function, so that the function "printf" can be implemented , and that is the role of the link.

function library is generally divided into static library and dynamic library two kinds. Static library refers to the compilation of links, the library files of the code are all added to the executable file, so the resulting file is relatively large, but at run time also no longer need the library file. The suffix is generally ". a". Dynamic libraries In contrast, when compiling a link does not add the code of the library file to the executable file, but the library is loaded by the runtime link file when the program executes, which can save the system overhead. The general suffix of the dynamic library is named ". So", as described in the previous libc.so.6 is a dynamic library. GCC uses dynamic libraries by default at compile time.
(The dynamic library file under Linux has the extension ". So" (Shared Object). By convention, all dynamic library filenames are in the form of libname.so (which may include a version number in the name). In this way, the thread function library is called libthread.so. The file name of the static library is libname.a. The file name of the shared archive is libname.sa. Shared archive is just a transitional form that helps people move from static libraries to dynamic libraries. )
Once the link is complete, GCC can generate the executable file

GCC compilation process

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.