1, a program compiled from the source file to build the executable file steps:
Precompiled--compile-and-link
(1) precompilation primarily deals with pre-compiled directives that begin with "#" in the source code file, such as macro expansion, processing of conditional compilation directives, processing # include directives, and so on.
(2) The process of compiling is a series of lexical analysis, grammatical analysis, semantic analysis and optimization of the pre-processed files into corresponding assembly code files.
(3) Assembly is the conversion of assembly code into machine instructions.
(4) The main link is to collect and synthesize scattered data and code into a single loadable file that can be executed. Links can occur when code is statically compiled, when the program is loaded, and when the program executes.
The main work of the linking process is symbolic parsing and relocation.
2. Library
A library is a package of target files that are packaged when some of the most commonly used code is compiled into a target file. The most common library is the runtime library, such as the C run-time library CRT.
Libraries are generally divided into two types:
static libraries (. A,. lib)
Dynamic libraries (. So,. dll)
The so-called static, dynamic refers to the link process.
3. Static link and static link library
Static Link: The source program generates the target file through the compiler, and the destination file and library generate the final executable file together. The process of linking is to combine the corresponding sections in each relocatable target file and complete the symbolic parsing and relocation.
Static Library: A collection of target files, that is, a file that is formed after the compressed packaging of many target files.
The static library can be used as input to the linker, and if the program needs to refer to a function provided by the static library, simply enter the library on the command line when linking. The connector copies only the target modules referenced by the program, and the target modules that the target module relies on.
Characteristics:
(1) Static link is completed at compile time.
(2) static link after the formation of the executable file, the runtime and the static library no longer related, easy to transplant.
(3) Waste memory and disk space. When an executable program is generated by a static link, all data and code in the static library that depends on it is copied into the executable program.
(4) Difficulties in updating. When the program relies on the static library has any updates, the entire program will be re-linked.
Create and use a static library under Windows:
Taking VS2010 as an example
Write a simple calculator class that is used by other programs in the static library, with the following code:
Ifndef calculator_h_#define Calculator_h_extern Double g_max_number;double Getminnumber (); Class Calculator{public: Double Add (double A, double b) const;double Sub (double num1, double num2) const;double Mul (double num1, double num2) const ;d ouble Div (double num1, double num2) const;}; #endif
CPP file
#include "calculator.h" double g_max_number = 999;double Calculator::add (double A, double b) Const{return (A + b);} Double Calculator::sub (double num1, double num2) Const{return (num1-num2);} Double Calculator::mul (double num1, double num2) const{return (NUM1 * num2);} Double Calculator::D IV (Double NUM1, double num2) const{if (num2! = 0) {return (num1/num2);} return 0;} Double Getminnumber () {return (-999);}
When you create a VS project, when you set the project properties, the configuration type selects the static library, and the build generates a static library.
4. Dynamic Link and dynamic library
C + + Static library and dynamic library