Four processes of GCC programming in Linux [reprinted]

Source: Internet
Author: User

Source: Linux community Author: Fu Dapeng 2008

To program C in Linux, you must use GNU gcc to compile C source code to generate executable programs.

1. GCC Quick Start

The general format of GCC commands is: 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! "

# Vi hello. c

# 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 From hello. C.

This command implicitly executes (1) preprocessing, (2) Assembly, (3) compilation, and (4) link to form the final binary executable program. The output file is not specified here. The default output is a. Out.

To specify the final binary executable program name, use the-O option to specify the name. For example, hello.exe

So

# GCC hello. C-o hello.exe

2. Analysis of GCC commands-four steps

From the above we know that the GCC compilation source code generates the final executable binary program, and the GCC background implicitly executes four stages.

There are four steps for GCC to compile C source code:

Preprocessing -----> compilation ----> Assembly ----> Link

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

Purpose: output the hello. c preprocessing file hello. I.

[Root] # gcc-e hello. C-O hello. I
[Root] # ls
Hello. c hello. I
[Root] # vi hello. I
#1 "Hello. c"
#1 "<built-in>"
#1 "<command line>"
#1 "Hello. c"
#1 "/usr/include/stdlib. H" 1 3
#25 "/usr/include/stdlib. 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
#26 "/usr/include/stdlib. H" 2 3
#3 "Hello. c" 2
Void main (void)
{
Printf ("Hello world! /R/N ");
}

2) Compiling)

The second step is the compilation phase. 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 checking that the code is correct, GCC translates the code into an assembly language. You can use the "-s" option to view the Code. This option is only compiled but not compiled to generate the assembly code.

Option-S

Usage: [root] # gcc-s hello. I-O hello. s

Purpose: Compile the pre-processing output file hello. I into a hello. s file.

[Root @ Richard hello-GCC] # ls

Hello. c hello. I hello. s

The following is the compilation code of Hello. S.

[Root @ Richard hello-GCC] # vi hello. s
. File "Hello. c"
. Section. rodata
. Lc0:
. String "Hello world! /R/N"
. Text
. Globl main
. Type main, @ Function
Main:
Pushl % EBP
Movl % ESP, % EBP
Subl $8, % ESP
Andl $-16, % ESP
Movl $0, % eax
Subl % eax, % ESP
Subl $12, % ESP
Pushl $. lc0
Call printf
Addl $16, % ESP
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 phase)

In the Assembly phase, the ". s" file generated in the compilation phase is converted to the binary target code.

Option-C

Usage: [root] # gcc-C hello. S-O hello. o

Purpose: Compile the output file test. s and output the file test. O.

[Root] # gcc-C hello. S-O hello. o

[Root] # ls

Hello. c hello. I hello. O hello. s

4) link stage)

After successful compilation, it enters the link stage.

No option Link

Usage: [root] # GCC hello. O-o hello.exe

Purpose: link the compiled output file hello.oto the final Executable File hello.exe.

[Root] # ls

Hello. c hello.exe hello. I hello. O hello. s

Run the executable file. The correct result is as follows.

[Root @ localhost GCC] #./Hello

Hello world!

This involves an important concept: function libraries.

Readers can re-view this applet. In this program, the function implementation of "printf" is not defined, 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 implements these functions named libc. so.6 is included in the library file. If it is not specified, GCC will search for it under the default search path "/usr/lib", that is, link to libc. go to the so.6 library function to implement the function "printf", which is the role of the link.

You can run the LDD command to view the dynamic library loading status:

[Root] # LDD hello.exe

Libc. so.6 =>/lib/tls/libc. so.6 (0x42000000)

/Lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000)

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.

Related Article

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.