Create and use static and dynamic libraries with visual studio 2019

After adding #include <WinSock2.h> in the cpp file, the compilation error shows that cin, cout, endl and some variables defined by you all display unidentified identifiers, as well as the following errors
1> C: \ Program Files (x86) \ Windows Kits \ 10 \ Include \ 10.0.17763.0 \ um \ WinSock2.h (2606,1): error C2375: "WSAAsyncGetHostByName": redefinition; different link, unknown reason.
Adding #include <WinSock2.h> is to realize the function of socket communication, but the compilation is not successful.
Solution: Put the socket communication function into a static library or a dynamic library.
Note: Do not write #include <WinSock2.h>, #include <WinSock2.h in the header file of the static library (assuming it is a static library.h) > Write it in the source file of the static library (assuming static library.cpp), otherwise it will report the same error, because you write #include "header.h" in the source program (assuming source.app), which is equivalent The #include <> in the static library .h is written here.

The following is the introduction and use of static and dynamic libraries

1. The difference between static and dynamic libraries

The exe program will integrate the static library when it starts to run, while the dynamic library is only called when the program needs it. lib is a static library file, and dll is a dynamic library file.
lib can realize different functions of the program.
The dll can realize different functions of the program, and the dll can be replaced or added when the program is upgraded, so that the function can be upgraded or added.

2. The use of static libraries (visual studio 2019)

1. Open visual studio 2019, choose to create a new project
Insert picture description here2. Select static library, the next step. Then name your project and create it.
Insert picture description here3. Writing of static library code. Create a static library in this way, visual studio 2019 will help you create two header files framework.h and pch.h and two source files pch.cpp and StaticLib1.cpp (named according to your project name), write your code to The corresponding place will do.
Note: Write the function declaration in the header file, such as pch.h here. In this way, you can include the function declaration by #include "... / pch.h" (absolute path or relative path) in the source file of the static library to be used. Otherwise, the compiler cannot recognize the function you wrote in the static library
Insert picture description here4. The generation of the static library. After writing the code, right-click the project-generate, the compiler will generate a debug folder in the project directory, which contains the following files.
Insert picture description here** 5. Use of static libraries. ** Create a new project, let's say a console program. First, add the header file of the static library through #include (note the path). Then right-click Project-Properties-Configuration Properties-Linker-General-Additional Library Directory. Add the directory where your lib file is located, either relative or absolute. Then through #pragma comment (lib, " your static library name.lib"), add the static library, you can use the functions of the static library.

3. Use of dynamic libraries

1. Create a dynamic library project. Select Dynamic Link Library (DLL).
Insert picture description here2. The entrance to the DLL application.
Insert picture description hereIn the project created by the compiler for you, there is a dllmain.cpp file, which defines the entry of the DLL application, as shown in the figure.
APIENTRY is defined as __stdcall, which means that this function is called in the standard Pascal manner, which is the WINAPI method; the
function parameter hModule is each DLL module in the process is identified by a globally unique 32-byte HINSTANCE handle (handle represents The starting address of the DLL module in the process virtual space is only valid within a specific process); the
parameter ul_reason_for_call indicates the reason for being called. There are four types, namely PROCESS_ATTACH, PROCESS_DETACH, THREAD_ATTACH, and THREAD_DETACH, listed as a switch statement;
lpReserved represents a reserved parameter, which is rarely used at present.

To call a function in Dll, a program must first map the DLL file to the address space of the process. To map a DLL file to the address space of a process, there are two methods: statically linked and dynamically linked LoadLibrary or LoadLibraryEx.
When a DLL file is mapped into the address space of the process, the system calls the DllMain function of the DLL, and the ul_reason_for_call parameter passed is DLL_PROCESS_ATTACH.
Other calling methods will pass different ul_reason_for_call parameters. Such as: When the DLL is unmapped from the address space of the process, the system calls its DllMain, and the value of ul_reason_for_call passed is DLL_PROCESS_DETACH. A single thread is started, and the DllMain function of the DLL is called with the value DLL_THREAD_ATTACH. A single thread terminates, use DLL_THREAD_DETACH to call the DllMain function.
3. Writing of dynamic library code.
Write a simple function function () in the dynamic library. This function is defined in the header file, and the source file implements the function. Right-click project properties-generate. The compiler generates a debug folder in the project directory, which contains dll files.
Insert picture description here
Insert picture description here4. The use of dynamic link library (implicit call)
first introduce a tool, Dependency Walker. It is a very useful PE module dependency analysis tool provided in Microsoft Visual C ++. The main functions are as follows:
view the import module of the PE module.
View the import and export functions of the PE module.
Dynamically analyze the module dependencies of PE modules.
Resolve the C ++ function name.
That is, you can analyze the dlls used by the program, and view the functions in the program or dll.
Open the dll file you created with Dependency Walker and find that there is no function because the function is not exported.
Insert picture description hereDeclare the exported function with __declspec (dllexport). Regenerate it. You can see that a lib file and an exp file are generated.
Insert picture description hereInsert picture description here
Reopen the dll file you created with Dependency Walker. In it, you will find the function you exported. Of course, the compiler renamed it. (This is caused by the different syntax of C language and C ++. The method of not changing the function name is introduced below, because the function name is used to call the function, which is inconvenient.)
Insert picture description hereDeclare the import in the app file of the program that needs to use the dynamic link library Functions (that is, some or all of the functions exported by the dynamic link library).
First include the header file of the dynamic link library in your app file. #include "… / Dll1 / pch.h"
Insert picture description here
declares import and export functions through conditional compilation instructions. Modify the dll header file.
Insert picture description here
Right-click dll project properties-C / C + ± preprocessor-preprocessor definition, _DLLAPI pre-defined.
Insert picture description hereIn this way, DLLAPI represents __declspec (dllexport) in dll, and DLLAPI represents __declspec (dllimport) in the app file that calls dll. Because you have no predefined _DLLAPI in the app file that calls dll.
In the program where you call the dll, right-click the project properties-configuration properties-linker-general-additional library directory, add the directory of the lib file generated by the dll project. Import the dll lib file from the dll app file. Through the statement #pragma comment (lib, "DLL1.lib"),
Insert picture description hereput your dll in the directory where the exe program calling the dll is located, and run the program calling the dll.
Insert picture description here5. Use of dynamic link library (display call)
Dynamically load dynamic libraries, do not use the statement #pragma comment (lib, "DLL1.lib"), the code of the app file that calls the dll is as follows:
Insert picture description hereNote the function name in it,? Function @@ YAXXZ, as we saw in Dependency Walker Function name
Insert picture description here Start executing the app that calls the dll.
Insert picture description here6. How to make the exported function not renamed The
first way The
compiler uses C ++ syntax when compiling the cpp file, which will change the name of the exported dynamic library function, and change the function to? Function @@ YAXXZ as above.
The compiler uses the C language syntax when compiling the C file, and does not change the name of the exported dynamic library function.
If you want to compile cpp, and you don't want to change the name of the exported function during compilation, you can add extern "C" before the function declaration to tell the compiler to use the C language compilation method to compile.
Insert picture description hereInsert picture description hereThis function has not been renamed.
The second way
uses module definition files. Right-click the source file under the dll project-add-new item, select the module definition file (.def). Create a new def file.
Insert picture description hereWrite the name of the dll module you want to export and the function name in the dll module in this file.
Insert picture description hereNote: With this def file, there is no need to export functions in the dll header file.
Insert picture description here
Rebuild the dll project. Check the exported function name in Dependency Walker.
Insert picture description here

Published 3 original articles · Likes0 · Visits 234

Guess you like

Origin blog.csdn.net/weixin_34007256/article/details/105346608