C + + Dynamic library encapsulation and invocation (1, dynamic Library Introduction)

Source: Internet
Author: User

1, a program compiled from the source file to build the executable file steps:

Precompiled--compile-and-link

(1) Pre-compilation, that is preprocessing, mainly processing in the source code files with "#" to start the pre-compilation instructions, such as macro expansion, processing conditional compilation instructions, processing # # # directives.

(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 binary files.

(4) Links link binary files into an executable command, mainly 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 generally fall into two categories: Static library (. A,. lib) dynamic library (. So,. dll), which is called static, dynamic refers to the link process.

3. Static Library and dynamic library

Difference:

(1) Lib is used at compile time, DLL is used at runtime. If you want to complete the compilation of the source code, you only need lib, if you want to make a dynamically linked program run, you only need the DLL.
(2) If there is a DLL file, then Lib is generally some index information, log the function of the DLL's entry and location, DLL is the specific content of the function, if only the Lib file, then the Lib file is statically compiled, the index and implementation are in it. Using a statically compiled Lib file, you do not need to hang the dynamic library when you run the program, the disadvantage is that the application is larger, and the flexibility of the dynamic library is lost, and the new version is released when the new application is published.
(3) In case of dynamic link, there are two files: one is lib file and one is DLL file. Lib contains the name and location of the function exported by the DLL, the DLL contains the actual functions and data, and the application uses the Lib file to link to the DLL file. In the application's executable file, it is not the called function code, but the address of the corresponding function code in the DLL, thus saving memory resources. DLL and LIB files must be released with the application or the application will produce an error. If you do not want to use the Lib file or no Lib file, you can use the WIN32 API function LoadLibrary, GetProcAddress load.

This is mainly about the advantages of dynamic libraries. --------

Static libraries: Functions and data are compiled into a binary file (usually with an extension of. LIB). In the case of a static library, when compiling the linked executable, the linker copies the functions and data from the library and combines them with other modules of the application to create the final executable file (. EXE file).
When using a dynamic library, there are often two files available: An introduction library and a DLL. The introduction library contains the symbolic names of the functions and variables exported by the DLL, and the DLLs contain the actual functions and data. When compiling the link executable file, only need to link to the library, the DLL's function code and data is not copied to the executable file, at run time, then load the DLL, access the DLL exported functions.

There are two major drawbacks to a static library:

1) Wasted Space

2) Static linking updates, deployments, and releases of programs can cause a lot of trouble. Once the program has any module updates, the entire program will be re-linked, published to the user.

The basic idea of dynamic linking is to split the program into separate parts according to the module, and link them together to form a complete program when the program is running, instead of linking all the program modules into a single executable file like static links.

Characteristics:

1) code sharing, all executable target files referencing the dynamic library share a copy of the same code and data.

2) The program is easy to upgrade, the application does not need to relink the new version of the dynamic library to upgrade, in theory simply to overwrite the old target file.

3) dynamically choose to load various application modules at run time

C + + Dynamic library encapsulation and invocation (1, dynamic Library Introduction)

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.