C language compilation in Linux (similar to Windows)

Source: Internet
Author: User

Similar reference:

Http://anony3721.blog.163.com/blog/static/51197420114173238237/ (Declaration: The following part of content and pictures are reproduced on this page)

Http://hi.baidu.com/fwy434822/blog/item/c6289e3541f6271590ef391e.html

Http://www.chineselinuxuniversity.net/articles/33950.shtml

Http://tieba.baidu.com/F? Kz= 663104251

Http://www.doc88.com/p-73741631829.html

Http://www.ibm.com/developerworks/cn/linux/l-pow-gnutool/ (GNU tool chain)

Summary:

Summarize the C language compilation process (from source code to executable files) in Linux, as well as the tool for the GNU tool chain and the options used.

1. Compilation and link Process

In the C language compilation link process, we need to compile and link a C program (source code) compiled by us to a program (executable code) that can be run on hardware. Compiling is the process of translating the source code in text form into a target file in machine language form. Links are the process of organizing the target file, the startup code of the operating system, and the library files used to generate executable code. The process is illustrated as follows:


From the figure, we can see that the entire code compilation process is divided into two processes: Compilation and linking. The compilation process contains the section enclosed in braces, and the rest is the link process.

The process of generating a file using the GNU tool chain is as follows:

Note: These suffixes are not mandatory. They are common suffixes. For C ++, they are generally. CPP ,. II ,. S ,. o, exec /. so. See http://zhidao.baidu.com/question/89958523.html. Of course, in essence,. c/. I/. s files are all text files and can be directly viewed .. O/exec/. So files need tools to view some information.

Ii. Three Phases of Compilation:

1. preprocessing phase
Before the formal compilation phase, the pre-processing phase will modify the content of the source file based on the pre-processing instructions that have been placed in the file. The main content of preprocessing includes:
A. macro definition, for example, # define PI 3.14
In short, macro replacement is used.
B. Conditional compilation, such as # ifdef, # ifndef, # else, # endif, etc.
The pre-compiled program filters out unnecessary code based on these commands.
C. header file inclusion, for example, # include <FILENAME. h>, # include "filename. H"
D. Special symbols. The pre-compiled program can recognize some special symbols.
Typical ones are the predefined macros built into the compiler, such as _ line _ and _ file.

In short, the core work of preprocessing is"Replace". Of course, preprocessing also removes the comments in the Code. In short, preprocessing aims to simplify the content scanned during compilation.

Corresponding GCC options:-E (preprocessing, but not compiling)The output is the result of preprocessing the. c file, which is output to the console by default and specified to be output to the file using-o. For the GNU tool chain, it providesIndependent pre-processor, CPP.

Example:

// File: test.h#define MACRO_A1int foo();
// File: test.cpp//#include <stdio.h>#include "test.h"#define MACRO_B"macro_B"int main(){printf("MACRO_A  is :%d; MACRO_B is: %s\n", foo(), MACRO_B);return 1;}int foo(){return MACRO_A;}

Compile:

gcc test.cpp -E -o out.ii

Note: In test. cpp, # include <stdio. h> is commented out for preprocessing. The printf error is returned only during compilation. The output content of out. II is as follows:

#1 "test. CPP "#1" <built-in> "#1" <command line> "#1" test. CPP "#1" test. H "1int Foo (); #4" test. CPP "2int main () {printf (" macro_a is: % d; macro_ B is: % s \ n ", Foo ()," macro_ B "); return 1 ;} int Foo () {return 1 ;}

Note: here we can see some information starting with #, which will be ignored (not scanned) during compilation, the contents of these rows are some information that records some rows and files. # Include <stdio. h> comment out Because stdio. the content in H will also be replaced with out. II. This is too much content and is not suitable for posting here.

If CPP is used for preprocessing, it means (GCC still calls CPP internally ):

cpp test.cpp -o out.ii

2. Compilation and Optimization
The pre-compiled output file contains only constants, such as numbers, strings, variable definitions, and keywords in C language, such as main, if, else, for, while, {,}, +,-, *, \, and so on.
Compilation refers to the content mentioned in the Traditional compilation principles, such as lexical analysis, syntax analysis, intermediate code generation, and assembly code generation. (Note: The intermediate code generation here is the code of the intermediate expression in the compilation principle, not the. O target file. It seems that the target file is also called an intermediate file, which is distinguished here)
Optimization is also involved in the compilation principle. Optimization involves a lot of content, optimization can be performed on the optimization of the intermediate code itself or the generation of the target code (that is, the process of generating the assembly code from the intermediate code or the process of generating the target file). For optimization, we will not discuss it further here.
In short, the compilation process described here is the process of optimizing the compilation of compiled files from the pre-processed files.

Corresponding GCC options:-S (compilation, but not Assembly). Output is a. s file. Note: GCC compilation can use source files as input, but can also directly use pre-processing. I files as input. Of course, the only thing to note is that if the original source file is CPP and the output is. I is not. II, so. when I is used as the input, it is best to use g ++, otherwise there may be compilation problems (GCC/g ++ will judge whether it is a C or C ++ File Based on the file suffix, so it works ).

For the GNU tool chain, it providesThe independent pre-compiler is CCL (does it seem to have this command? In short, CCP and CCL are rarely used, but GCC can be used directly ).

Or the above example (uncomment # include <stdio. h> ):

$ gcc test.cpp -E -o test.ii$ gcc test.cpp -S$ gcc test.ii -S$

Note:-S is output to the file by default, So-O can be output to the file without being used.

3. Assembly

Compilation actually refers to the process of translating assembly language code into target machine commands. The output is the target file (. O ). What is stored in the target file is the machine language code equivalent to the source program. The target file consists of segments. Generally, a target file contains at least two segments:
Code segment: This section mainly contains program instructions. This section is generally readable and executable, but generally cannot be written.
Data Segment: It mainly stores various global variables or static data used in the program. Generally, data segments are readable, writable, and executable.

Note: After compilation, the compilation process is completed and the target file (. O) is obtained ).

Corresponding GCC options:-C (assembly, but not linked).

Or the above example (it is okay to use. II/. S/. cpp as the input. GCC will know what file the input is based on the suffix, so as to continue processing on this basis ):

$ gcc test.ii -c$ gcc test.s -c$ gcc test.cpp -c$ 

Among them, GCC test. S-C is to directly compile the Assembly file, while GCC test. cpp-C will start to process from preprocessing. For the GNU tool chain, it providesIndependent assembler,. You can also use as to compile the Assembly file (of course, you can only compile the Assembly file). The above GCC test. S-C is equivalent:

$ as test.s -o test.o

(If-O test. O is not used, the output is a. o by default)

Summary: The compilation mainly includes three stages: preprocessing, compilation, and assembly. The GCC option can be used to control the GCC processing to stop after a certain step. Of course, the actual GCC processing, it may not be completely processed step by step, but there may be some optimized processing methods.

3. Links

The target file generated by the assembler cannot be executed immediately. There may be many unsolved problems.
For example, a function in a source file may reference a symbol (such as a variable or function call) defined in another source file, or call a function in a library file in a program. All these problems must be handled by the linked program.
The main task of linking a program is to connect the target file to each other, or connect the symbols referenced in one file with the definition of the symbol in another file, this makes all these target files a unified whole that can be loaded and executed by the operating system.

(1) Static Link
In this connection mode, the code of the function will be copied from its static Link Library to the final executable program. In this way, when the program is executed, the code will be loaded into the virtual address space of the process. The static Link Library is actually a collection of target files. Each file contains the code of one or more related functions in the library.
(2) Dynamic Link
In this way, the function code is put in a target file called a dynamic link library or shared object. What the linked program does at this time is to record the name of the shared object and a small amount of other registration information in the final executable program. When the executable file is executed, all content of the dynamic link library will be mapped to the virtual address space of the corresponding process at runtime. The dynamic link program finds the corresponding function code based on the information recorded in the executable program.

For function calls in executable files, dynamic or static links can be used respectively. Dynamic Links can make the final executable files relatively short, and save some memory when the shared object is used by multiple processes, because only one copy of the shared object code needs to be saved in the memory. However, dynamic links are superior to static links. In some cases, dynamic links may cause some performance damage.

Iv. Overall process

The following is the overall process:

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.