Understanding and use of static library (lib) and dynamic library (dll)
Preface
Static libraries and dynamic libraries are very commonly used in windows software development. Many functions of a good software architecture are in the form of libraries. If the function needs to be upgraded, the library file can be directly replaced without upgrading the entire program. This is more convenient to maintain.
Common library files have two forms, .lib and .dll , which are static libraries and dynamic libraries.
When the static library is formed into an exe file, it is all packaged into it.
The dynamic library is only imported into the exe when it is running, and can be loaded and unloaded dynamically.
One, the understanding of function overloading
C++ supports function overloading, but C does not support function overloading.
After the functions of the same name in C are compiled, the function names are the same. Taking VS as an example, they all start with an underscore "_", so if you call, you don't know which function to call, so C does not support overloading.
Cdecl is this kind of calling method, directly looking for the original function name, without adding any decoration to the function name.
After the function of the same name in C++ is compiled, the compiler will add meanings such as parameter type and parameter number to the function name to distinguish, that is, each function name is different and can be distinguished, so C++ supports overloading. (The naming rule in C++: Name-Mangling)
(Each function with the same name with different parameters shown in the picture below has a different name for the function compiled later)
This calling method of stdcall will add parameters and other decorations to the original function name. Used to distinguish the same function function but the function body and parameters are different when the function is overloaded.
2. Understanding of extern "C"
As mentioned above, after C++ compiles a function, its name will be modified again, adding parameter information, then C++ function and C function cannot call each other, because the names of their respective functions are different after compilation, and they will be found when linking. An error is reported if the function name is not available . But in practice, it is possible, indicating that certain processing has been carried out.This is the role of extern "C".
Its role is to generate the function name in the same way that the function name is generated in C.
In this case, even if the respective compilers do not use the method of generating function names, they are all generated according to the C standard , so they can call each other. Therefore, if the dynamic library is to be called externally, this modification is generally required .
Three, the call of the static library
1. The generation of static library
(1) Generate static library (lib)
First click on the static library (Visual C++), then after selecting the directory, click OK.
You can remove other files, leaving only pch.h and pch.cpp,
Static library .h program
#ifndef PCH_H
#define PCH_H
#include <iostream>
void Print();
int Add(int a, int b);
#endif //PCH_H
Static library.cpp program
#include "pch.h"
using namespace std;
void Print()
{
cout << "I am a smart boy!" << endl;
cout << "666!" << endl;
cout << "you are a pretty girl!" << endl;
}
int Add(int a, int b)
{
return ((a)+(b));
}
Finally, click Generate, and you will see the lib file is generated.
2. The call of static library
There are three ways, program import, resource file import, and compiler import.
Regardless of the import method, you need to import the header file of the library . In this article, you need to add the pch.h file, and the path of its .h also needs to be added, in the project-"Properties-"C/C++ directory-"Additional directory Add the directory of the .h file.
- Import through the program (the path needs to use \ \ )
#include <iostream>
#include "pch.h"
#pragma comment(lib, "D:\\guoqing.zhang\\StaticLibrary\\StaticLib\\Debug\\StaticLib.lib")
int main()
{
Print();
std::cout << Add(3, 6) << std::endl;
}
- Resource file import
- Compiler import
Project-"Properties-"Linker-"Additional library directory in the general, to add the static library path.
Project-"Properties-"Linker -" input additional dependencies, you need to add the name of the static library.
No matter what kind of addition is completed, add "pch.h" to the main function file, click Debug, and you will see the running result.
Fourth, the call of the dynamic library
1. Dynamic library generation
Click Project-"New Project-"Dynamic Link Library DLL, and the project can be generated after selecting the path. You can leave only one .h and one .cpp file, and the others can be deleted.
Dynamic library.h program
#pragma once
#ifndef DLL_H
#define DLL_H
#define LIB_API extern "C" __declspec(dllexport)
#include <iostream>
LIB_API void Demo();
LIB_API int Sub(int a, int b);
#endif //PCH_H
Dynamic library .cpp program
#include "Dll.h"
#include "pch.h"
LIB_API void Demo()
{
Print();
std::cout << Add(3, 5) << std::endl;
}
LIB_API int Sub(int a, int b)
{
return ((a)-(b));
}
That's right, after you read it, you find that the dynamic library can call the static library. The Dll library in this article calls the StaticLib.lib generated before, adding the header file of the static library.
This is also the difference between a static library and a dynamic library . A dynamic library can include other static libraries or dynamic libraries, while a static library cannot include other static libraries or dynamic libraries.
After programming, click Generate, you can see that there is a .dll library generated, enter its folder, you will find that there is a corresponding .lib file generated, this is not a library program, just the function name and location of the dll file , which is convenient to find the .dll Function entity.
If you find an error such as the error in the figure below, then it is a problem with the precompiled header, and you can complete the compilation normally by choosing not to use the precompiled header. (The precompiled file is deleted.)
2. The call of dynamic library
Implicit call : put Dll.dll, Dll.lib and Dll.h under the project directory.
Linker-"Enter to add additional dependencies, select Dll.lib,
Dll.lib can be added to the resource file or attachment dependency , and then add the "Dll.h" header file to the main function, call the function in the library,
click Debug, and it can run normally.
Explicit call : without adding .h files and .lib files, directly import the dll library, and call the functions in the library through the function pointer. The specific process is as follows:
1. Import the dll library LoadLibrary (library name)
2. The constructor pointer must be consistent with the function in the library file, including function parameters and types.
3. Get the function address through the function pointer, GetProcAddress (handle, function name/function number); (function number: the order in which the function is declared in .h)
4. Use the function pointer to call the function
#include <iostream>
#include <Windows.h>
int main()
{
std::cout << "Hello World!\n";
//1、导入dll库
HMODULE h = LoadLibrary(L"Dll.dll");
//2、构造函数指针
typedef int(*PSUM) (int, int);//
//3、利用函数指针调用函数
PSUM psum =(PSUM)GetProcAddress(h,"Sub");//利用函数名
//PSUM psum = (PSUM)GetProcAddress(h, (char*)2);//利用函数序号
std::cout << psum(11, 22) << std::endl << std::endl;
typedef void(*PDemp) ();//
//3、利用函数指针调用函数
/*PSUM psum =(PSUM)GetProcAddress(h,"sum");*///利用函数名
PDemp pdemo = (PDemp)GetProcAddress(h, (char*)1);//利用函数序号
pdemo();
//4、释放DLL
FreeLibrary(h);
return 0;
}
Called successfully.
If it is found that the function name cannot be found and the dynamic link cannot be completed, then the function name in the formed library may not match the linked function name . (There is one that has been modified by adding information, and the other is unmodified.) (error in the picture below)
Get the function in the dll through the GetProcAddress function. The function exported in the dll must be decorated with extern "C", otherwise the function address cannot be found. (The reason is that C++'s Name-Mangling modifies the function name rule ).
Example code download