C / C ++ topic - compilation

+ Topics: a C ++ source file experience from a text file into the executable process?

【answer】

For C ++ source files, executable files from text to generally require four processes:

Preprocessing phase: the source code file containing the file relationship (header), prepared statement (macros) and analyzed Alternatively, pre-compiled file generation.

Compile stage: converting the pre-compiled into a specific file preprocessed assembly code to generate the assembler file

Compilation Phase: The compile stage conversion into machine code generated assembler file, generate relocatable object files

Link stage: multiple object files and libraries required to connect into a final executable object file

+ Topic: Tell me about the first document include double quotes "difference" and angle brackets <> of?

【answer】

This is because the compiler pre-processing stage to find inconsistencies order header file path.

Find the order of double quotes header file:

1. The compiler first looks for the current project directory of header files (usually the case, the header file is our custom header file)

2. Then is the header file path compiler settings

3. The system variables specified header file path

Find the order of angle brackets header file:

1. The header file path compiler provided

2. System variables specified header file path

Title: source to process executable file? (From a blog post)

[ Answer ]

1 ) pre-compiled

The main source code file to " # " at the beginning of the pre-compiler directives. Processing rules below

1 , remove all #define , expand all the macro definitions.

2 , all the processing conditions of pre-compiled instructions , such as the " #if ", " #endif ", " #ifdef ", " #elif " and " #else ."

3 , the process " #include " pre-compiler directive to replace the contents of the file to its position, the process is recursively performed, file contains other files.

4 , delete all comments " // " and " / ** / ."

5 , retain all the #pragma compiler directives , the compiler need to use them, such as: #pragma Once is to prevent any files have been repeated reference.

6 , adding the line number and file identifier , to facilitate the compiler when the compiler generates line number information for debugging, and compile-time compiler error or warning is generated can be displayed line number.

2 ) Compile

After the generated precompiled xxx.i or xxx.ii file, a series of lexical analysis, syntax analysis, the semantic analysis and optimization, to form the corresponding assembly language file .

1 , lexical analysis: using a similar "finite state machine" algorithm, the source program is input to the scanner in which the character sequence is divided into a series of tokens.

2 , syntax analysis: parser on tokens generated by the scanner, syntax analysis, generates a syntax tree. Output from the parser syntax tree is a node in the expression tree.

3 , semantic analysis: parser just completed an analysis of the level of expression syntax , semantic analyzer for the expression if it makes sense to judge, its semantic analysis is static semantics - in installments can compile the semantics phase the corresponding dynamic semantics can be determined at runtime semantics.

4 , the optimization: a source-level optimization process.

5 , the object code generated: by the code generator converts the intermediate code into machine code target, generate a series of code sequence - assembler language representation.

6 , the target code optimization: object code optimizer above objects optimized machine code: finding suitable addressing mode, instead of using the displacement multiplication, and the like to remove the extra instructions.

3 ) Compilation

The assembly code into machine instructions can be executed ( machine code files ) . Assembler assembly process is relative to the compiler is more simple, not complex syntax, nor semantics, but do not need instruction optimization, just eleven translation table according to the assembly instructions and machine instructions, assembly process there assembler as complete. By then compiled to produce an object file ( executable file format is almost the same ) xxx.o (Windows under ) , xxx.obj (Linux under ) .

4 ) Link

The object file produced by a different source link, so as to form a program that can be executed. Divided into static and dynamic links link link:

1 , static link:

Functions and data is compiled into a binary file. In the case of static library, when compiled and linked executable file, the linker functions and data copied from the library and put them to other modules and applications combined to create the final executable file.

Waste of space : because every executable program must have a copy of all the required target files, so if you have more than one program relies on the same target file, there will be the same object file in memory there are multiple copies ;

Update difficult : Whenever the code library function changes, and this time will need to be recompiled linked to form an executable program.

Running speed : statically linked, but the advantage is that the executable program has had anything to perform all procedures required to run fast at the time of execution.

2 , dynamic link:

The basic idea is dynamically linked to the program module in accordance with independent split into individual portions, until the program runs in the link them together to form a complete program, rather than as statically linked program modules are all linked to a single executable file.

Shared libraries: that even if the needs of each program are dependent on the same library, but the library does not exist as points on a copy in memory like static links, but several programs which, when executed share the same copy ;

 

+ Title: header file ifndef / define / endif what for?

【answer】

The file header is to prevent duplicate references.

Update convenient: only need updating replace the original target file , and without having to re-program all the links again. When run under the program, the new version of the target file will be automatically loaded into memory and link up program to complete the upgrade of the goal.

Performance Loss: Because the link postponed until the program is running, so each execution procedures need to be linked , so there will be some loss of performance.

+ Topic: What is pre-compiled, pre-compiled when you need it?

【answer】

1. Always use a large body of code are not changed often.

2, a plurality of program modules, all modules use a standard set of files containing the same compilation options. In this case, all the files can be pre-compiled comprising a precompiled header.

Title: What are the implications volatile keyword and give three different examples?.

【answer】

Defined as a variable volatile is that this variable may be changed unexpectedly, so the compiler would not have to assume that the value of this variable. Precisely that, every time the optimizer must be carefully re-read the value of this variable when this variable is used, instead of using a backup stored in registers. Here are a few examples of volatile variables:

1) Hardware registers devices (eg: Status Register)

2) An interrupt service routine access to the non-automatic variable

(Non-automatic variables)

3) variables in multi-threaded applications to be shared by multiple tasks

Topic: Call function after being compiled C compiler in a C ++ program, why should we increase extern "C"?

【answer】

C ++ language support function overloading , C language does not support function overloading . Function is different from the name of the C language in the library of the C ++ compiler.

Suppose a function prototype: void foo (int x, int y);

 

Guess you like

Origin blog.csdn.net/chen1083376511/article/details/92071180