Dynamic link library in C++ examples of imports and exports
- 2020-05-30 20:46:35
Dynamic link libraries in C++ - examples of imports and exports
__declspec (dllexport) and __declspec (dllimport) :
__declspec(dllexport) : the compiler sees 1 Variables, functions, or C++ The class is decorated by it, so it knows what should be generated DLL Exports the variable, function, or C++ Class.
__declspec(dllimport) : the compiler sees 1 Variables, functions, or C++ Class is decorated by it, so it knows the executable or DLL The source files need to be from others DLL Import in module 1 Variables and functions.
Import segment of DLL:
When building an executable module, the executable module can also contain an import segment that lists the names of all the DLL modules it needs. For each DLL listed,
This section also records the symbolic names of the functions and variables referenced in the executable's base 2 code. You have the same import in DLL,
Record the module name and function, symbol name it needs.
DLL file export section:
The linker generates an DLL file with an export symbol table embedded in it. This export section lists the names of the exported variables, functions, and classes.
extern "C" functions:
extern "C" is used to tell the compiler not to change variable or function names, so that the variable or function can be accessed by an executable module written in C/C++ or in any programming language.
Create DLL compatible:
C++ Support function overloading, C The language does not support function overloading, C++ The compiler changes the name of the function C Languages don't.
In order to use Microsoft Toolkit building 1 Can be linked to other compiler vendor toolkits DLL , you must tell the compiler not to adapt the name of the exported function.
This can be done in the following ways:
1 , extern "C" Modifies derived functions, variables, and classes;
2 , for the project 1 a .def File;
func-name @ 1
.def The rules of the document are:
1> EXPORTS The name of the function to be exported is listed after the statement .def The name of the exported function in the file is appended @n , means the sequence number of the function to be derived is n ;
2> .def The comments in the file are specified by a semicolon at the beginning of each comment line, and the comments cannot be Shared with the statement 1 Line;
3 , DLL Add: #pragma comment(linker, "/export:MyFunc=_MyFunc@8")
If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!