Basics of linux (III) Gcc Compilation and programming

Source: Internet
Author: User
Gcc Compilation 1 file type cC original program o target file h header file I pre-processed C original

Gcc Compilation

1. file type

. C C original program

. O target file

. H header file

. I pre-processed C raw file

. Ii pre-processed C ++ raw files

. A compiled static library file

. So compiled dynamic library file

Static library: all the code of the library file is added to the executable file when the link is compiled. the file is large and the library file is required for running.

Dynamic library: links files to load the library when the program runs, saving system overhead

2. Gcc compilation process

(1). Pre-compile

Gcc-E linux. c-o linux. I

Gcc [option] file [option] target file

Note: The target file can be defaulted.

Before pre-compilation:

 

After pre-compilation:

Compare the differences between the two files to get the pre-compilation work:

1) import: delete# IncludeAnd add the content of stdio. h to the program to respond to it,

2) replacement: poor deletion# DefineAnd replace all PI of the file with the specified value.

3)Replace the comment with a space character

4)DeleteSomeUnnecessary spaces

Ps: this. I file helps debug the error program

2. Compile

 

 

1) check the code normalization and syntax errors

2) translate into assembly code

3. Assembly

Is to convert the assembly code to the binary target code of ". o,

 

4. links

There is no function printf implementation in the source file. even in the pre-compiled file, only the printf function declaration is available, and the system places these functions in libc. so.6 is included in the library file, without special settings, GCC will go to the system's default search path "/usr/lib", that is, link to libc. so.6 library files (loaded as dynamic libraries)

 

 

Finally generate an executable file

 

Command parameter description:

-E only performs pre-compilation without any additional processing.

Gcc-E linux. c-o linux. I

-O file: uploads the output to the file.

Gcc-S linux. I-o linux. s

-S is just for compilation without assembly, and the assembly code is generated

Gcc-c linux. s-o linux. o

-C: only compilation is not linked, and the target code is generated.

Gcc linux. o-o linux

The previous steps are step-by-step execution to illustrate the entire compilation process. if you want to output them in one step, you can:

Gcc linux. c-O output file

 

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.