Four-step GCC compilation Standard

Source: Internet
Author: User
Standard ansi c: This standard is the C language standard set by ANSI (US National Standards Agency) in 1989. It was later accepted as a standard by ISO (International Organization for Standardization) and therefore known as iso c. Ansi c aims to provide portability guarantee for C Programs on various operating systems, not limited to Unix. This standard not only defines the language and semantics of the C programming language, but also defines a standard library. This library can be divided into 15 parts based on the header file, including: character type (<ctype. h>), error code (<errno. h>), floating point constant (<float. h>), mathematical constant (<math. h>), standard definition (<stddef. h>), standard I/O (<stdio. h>), tool functions (<stdlib. h>), string operation (<string. h>), time, and date
(<Time. h>), variable parameter table (<stdarg. h>), signal (<signal. h>), non-local jump (<setjmp. h>), local information (<local. h>), program assertions (<assert. h>. POSIX: The standard family originally developed by IEEE, which has been accepted by ISO as an international standard. For details about the standard, see 1.1.3. Posix.1 and posix.2 define POSIX-Compatible C language system interfaces and shell and tool standards. These two standards are commonly mentioned. Svid: System V interface description. The System V interface description (svid) is a document describing the AT & t unix System V operating system. It is an extended superset of POSIX standards. Xpg: X/Open portability guide. The X/Open portability Guide (published by X/Open Company, Ltd.) is a more general standard than POSIX. X/open has UNIX copyrights, while xpg specifies that the UNIX operating system must meet the requirements. ---------------------------------------   GCC is short for GNU cc. It is a compilation system that complies with the ansi c standard and can compile programs written in C, C ++, Object C, and other languages. GCC is also a cross-platform compiler that can develop software for a variety of hardware platforms with different architectures on the current CPU platform, so it is suitable for development and compilation in the embedded field.  Explanation of the suffixes supported by GCC. C original program. c/. CC/. cxx C ++ original program. M Object C original program. I Original C program that has been preprocessed. II Original C ++ program that has been preprocessed. S/. s Original assembly language program. H Pre-processing file (header file). O Target File. A/. So Compiled library files      Iii. GCC compilation process    Use VI to write the source file hello. C.    # Include <stdio. h>Int main (){   Int I;    For (I = 1; I <9; I ++)    Printf ("Hello world % d times! \ N ", I );    Return 0;}     1. preprocessing phase    The purpose of this phase is to compile the pre-processing file, that is, the header file. In this example, stdio. H is compiled. You can use the-E Option to check whether GCC stops the compilation process after preprocessing.[Armlinux @ lqm program] $ gcc-e hello. C-O hello. I [armlinux @ lqm program] $ cat hello. I | less #1 "hello. C "#1" <built-in> "#1" <command line> "#1" hello. C "#1"/usr/include/stdio. H "1 3 #28"/usr/include/stdio. H "3 #1"/usr/include/features. H "1 3 #291"/usr/include/features. H "3 #1"/usr/include/sys/cdefs. H "1 3 #292"/usr/include/features. H "2 3 #314"/usr/include/features. H "3 #1"/usr/include/GNU/stubs. H "1 3 #315"/usr/include/features. H "2 3 #29"/usr/include/stdio. H "2 3 ... Extern void funlockfile (File * _ stream); #679 "/usr/include/stdio. H" 3 #2 "Hello. c" 2int main (){   Int I;    For (I = 1; I <9; I ++)    Printf ("Hello world % d times! \ N ", I );    Return 0;}     It can be seen that the content of stdio. H has been inserted into Hello. C, that is, GCC completes the preprocessing process.    2. compilation phase    GCC first checks the code standardization and syntax errors to determine what the code actually needs to do. After checking the correctness, translate it into an assembly language. You can use-s to view the data, that is, compile only without entering the Assembly stage.[Armlinux @ lqm program] $ gcc-s hello. I-O hello. s [armlinux @ lqm program] $ cat hello. s   . File "Hello. c"    . Section . Rodata. Lc0:   . String "Hello world % d times! \ N"    . Text. Globl main   . Type Main, @ FunctionMain:   Pushl % EBP    Movl % ESP, % EBP    Subl $8, % ESP    Andl $-16, % ESP    Movl $0, % eax    Subl % Eax, % ESP    Movl $1,-4 (% EBP). L2:   CMPL $8,-4 (% EBP)    Jle . L5    JMP . L3. L5:   Subl $8, % ESP    Pushl -4 (% EBP)    Pushl $. Lc0    Call Printf    Addl $16, % ESP    Leal -4 (% EBP), % eax    Incl (% Eax)    JMP . L2. L3:   Movl $0, % eax    Leave    RET. Lfe1:   . Size Main,. Lfe1-main    . Ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5 )"      3. Assembly stage    GCC converts the compiled. s file to the target file. O. Then you can use the-C option to see that the assembly code has been converted to the. O target code.[Armlinux @ lqm program] $ gcc-C hello. S-O hello. o   4. Link stage    After successful compilation, it enters the link stage. First, we need to understand the concept of "Database. This program does not have the function implementation of "printf". In the "stdio. h" contained in the preprocessing phase, only the declaration of this function is available, but the implementation of the function is not defined. How to implement "printf "? The answer is: The system implements these functions with the libc name. so.6 is included in the library file. If this parameter is not specified, GCC searches for it under the default search path "/usr/lib. That is, link to the libc. so.6 library function to implement the function "printf", which is the role of the link.    The function library in RedHat 9 is as follows:    /Lib: required shared library    /Usr/lib: Standard shared library and static library    /Usr/x11r6/lib: x11r6 function library    /Usr/local/lib: Local function library    Header file:    /Usr/include: System header file    /Usr/local/include: Local header file    The/etc/lD. So. conf file contains the shared library search location. My libc. so.6 is under/lib and it is a link pointing to the libc-2.3.2.so. This is because the glibc version I used is 2.3.2.    Function libraries can be divided into static libraries and dynamic libraries. Static library refers to adding all the code of the library file to the executable file during the link. Therefore, the generated file is large, but the library file is no longer needed during the runtime. The suffix is generally ". ". The dynamic library does not add the code of the library file to the executable file when it is linked, but loads the library by linking the file at runtime during program execution, which can save the system overhead. The dynamic library is generally suffixed with ". So ". By default, dynamic libraries are used for GCC compilation.  [Armlinux @ lqm program] $ GCC hello. O-o hello1 [armlinux @ lqm program] $ LS-l hello1-rwxrwxr-x 1 armlinux 11582 August 28 17:42 hello1[Armlinux @ lqm program] $ file hello1hello1: Elf 32-bit LSB executable, Intel 80386, Version 1 (sysv), for GNU/Linux 2.2.5, dynamically linked (uses SHARED libs ), not SD [armlinux @ lqm program] $ gcc-static hello. o-O hello2 [armlinux @ lqm program] $ LS-l hello2-rwxrwxr-x 1 armlinux 423442 August 28 17:43 hello2[Armlinux @ lqm program] $ file hello2hello2: Elf 32-bit LSB executable, Intel 80386, Version 1 (sysv), for GNU/Linux 2.2.5, statically linked, not stripped [armlinux @ lqm program] $. /hello1hello world 1 times! Hello World 2 times! Hello World 3 times! Hello World 4 times! Hello World 5 times! Hello world 6 times! Hello world 7 times! Hello world 8 times! [Armlinux @ lqm program] $./hello2hello world 1 times! Hello World 2 times! Hello World 3 times! Hello World 4 times! Hello World 5 times! Hello world 6 times! Hello world 7 times! Hello world 8 times!   Hello2 is a static compilation with a size of 423442, 36.56 times that of dynamic compilation. The file can be used to see whether the file statically linked or dynamically linked (uses SHARED libs ).      Now, the entire GCC compilation link process is complete.

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.