After a long time, I always like to forget things, and then I have to go online to search for them. I have to write a bunch of things on the Internet, but I still need to record them myself. Today I will briefly describe the dynamic link library.
First, the database is divided into static and dynamic databases.
1. The functions in the static link are loaded into the exe file, and the DLL file is not required when the program is released. The program generates a. lib file.
2. dynamic Links are called runtime calls. They load functions only when used, and are shared in the memory. They do not add the DLL Code links to the program, therefore, when a program is released, it must contain a DLL file that has been loaded. However, this is advantageous, easy to expand, and easy to perform secondary development. The program generates a. lib file and A. dll file.
Conclusion: We found that both static and dynamic libraries have lib files. What is the difference between the two? Actually, two are completely different. The lib file corresponding to the static library is called the static library, and the lib file corresponding to the dynamic library is called the imported library. In fact, the static library itself contains the actual execution code, symbol table, and so on. For the import and export operations, the actual execution code is located in the dynamic library, the imported database only contains the address symbol table to ensure that the program finds some basic address information of the corresponding function. In the dynamic library, many third-party extension libraries are used in actual development, which are generally provided by others :. h file class, function declaration ),. dll file class or function implementation ),. lib file imported to the database), you can also do not provide the lib file, so you need a function pointer.
Then, the dynamic link library is loaded by explicit link and implicit link.
1. The implicit call is relatively simple, but it requires import to the database, that is, the. lib file, and then add a pragma comment (lib, "xxx. lib "). Or add your XXX. lib directly to the set Option in VC. Note that the dll file must be in the current project directory. Otherwise, the compilation may be correct and an error will occur during running.
2. while the display call is troublesome, it is not necessary. lib file, directly loadLibrary when needed, then declare the pointer to, and then use FreeLibrary after use. Although it is troublesome, sometimes it is more flexible than implicit link, for example, if the DLL cannot be found during running, the program can display an error message and continue running. explicit links are also useful when you want to provide plug-in services for your program.
HMOUDLU hinst = LoadLibrary ("XXX. dll ");
Typedef int (* ADDPROC) (int, int );
ADDPROC add = (ADDPROC) GetProcAddress (hinst, "add ");
Printf ("% d", add (2, 3 ));
FreeLibrary (hinst );
The two are the differences between the loading methods.
Well, there is nothing else. Let's put a simple example here. Let's take a look at it and you will understand the dynamic connection library. There is nothing else. It's super simple, HOHO.
Extern "C" declspec (dllexport) int add (int a, int B)
{
Return a + B;
}
Taking a look at the above theory, we have a very simple summary step:
1.Static libraries include. lib and. H files. There are three steps to use static libraries in the project:
(1) contains the lib file, pragma comment ();
(2) include. H files in the project;
(3) Use functions in the static library in the project;
2. The dynamic link library generally includes the. lib (export function),. h, and. dll files. There are two scenarios for using the dynamic library:
(1) implicit links, similar to static libraries, are divided into three steps: reference. lib, including header files, using the export function;
(2) Dynamic Loading: directly use LoadLibrary to load the required dynamic library, and then specify the required export function, with the highest efficiency!
This article is from the "West sun" blog, please be sure to keep this source http://thgenius.blog.51cto.com/1042803/1270837