The difference and usage of static library lib and dynamic dll in windows

1. Difference between static library lib and dynamic dll

1.1 Project Type

When VS is building a Win32 project, there are the following options:

  • windows application
  • console application
  • DLL
  • static library

The last two types: DLL and static library. These two project types cannot be run independently. They must be called from Windows applications for execution. They are just provided library functions.

1.2 The difference between the two libs:

(1) Static library (.lib)

Functions and data are compiled into a binary file (usually with a .LIB extension). In the case of static libraries, when compiling the linked executable, the linker copies these functions and data from the library and combines them with other modules of the application to create the final executable (.EXE file). When releasing a product, only the executable needs to be released, not the static library used.

(2) Dynamic library (.lib file and .dll file)

When using a dynamic library, two files are often provided after compilation: an import library (.lib) file (also called an "import library file") and a DLL (.dll) file. Of course, I will tell you later that if only one DLL file is provided, it can also be called by using the display connection method, but it is just a little troublesome.

Although the suffix name of the imported library is also "lib", there is an essential difference between the imported library file of the dynamic library and the static library file . For a DLL file, the import library file (.lib) contains the symbolic names of the functions and variables exported by the DLL, and the .dll file contains the actual functions and data of the DLL. In the case of using a dynamic library, when compiling and linking an executable file, you only need to link the import library file of the DLL, that is, the LIB. The function code and data in the DLL cannot be copied to the executable file until the executable program runs. , to load the required DLL, map the DLL to the address space of the process, and then access the exported functions in the DLL. At this time, when releasing the product, in addition to releasing the executable file, it is also necessary to release the dynamic link library to be called by the program.

Only when the EXE program really wants to call these DLL modules, the system will load them into the memory space. This approach not only reduces EXE file size and memory space requirements, but also enables these DLL modules to be used by multiple applications at the same time. If the DLL is not in memory, the system loads it into memory. When linking a Windows program to produce an executable, you must link against the special "import library" provided by the programming environment. These import libraries contain the dynamic link library name and reference information for all Windows function calls. The linker uses this information to construct a table in the .EXE file, which Windows uses to translate calls into Windows functions when the program is loaded.

The difference between the introduction library LIb and the static library Lib:

The difference between the import library and the static library is very big, they are actually different things. The static library itself contains the actual execution code, symbol table, etc. For the imported library, the actual execution code is located in the dynamic library, and the imported library only contains the address symbol table, etc., to ensure that the program can find some basic functions of the corresponding function. Address information. However, the import method of importing library files is the same as that of static libraries, and the path to find these .libs should be added to the link path.

1.3 The difference between dynamic library (Lib and DLL) and static library Lib: 
In fact, the difference has been mentioned above, here is a summary: 
(1) Both static link library and dynamic link library share code. If static link library is used, The instructions in lib are all directly included in the final EXE file, and the final executable file exe will be relatively large. However, if a DLL is used, the DLL does not have to be included in the final EXE file, and the EXE file can be "dynamically" referenced and unloaded when the EXE file is executed. 
(2) Another difference between a static link library and a dynamic link library is that the static link library cannot contain other dynamic link libraries or static libraries, and the dynamic link library can also contain other dynamic or static link libraries. The general comparison between static link library and static link library calling rules is as follows.

The static link library is loaded before running, and it exists until the program is closed. The dynamic DLL is loaded at runtime without occupying memory all the time. If the dll module is changed internally, it is necessary to replace the Dll, which is convenient for maintenance. The advantages are obvious, but if the dll is lost or deleted by mistake, it will not work


2. Use of static library lib and dynamic dll

2.1 The use of dynamic dll

The use of the dynamic link library requires the developer of the library to provide the generated .lib files and .dll files. Or just provide the dll file. When using, only the functions exported in the dll can be used, and the unexported functions can only be used inside the dll. There are two types of Dll calls: explicit connection and implicit connection: Implicit connection requires three things, namely *.h header file, lib library (dynamic), and DLL library ; display connection only requires .dll files .

2.1.1 Implicit link  
Implicit link requires three things, namely *.h header file, lib library (dynamic), DLL library , and the lib library here is only used when compiling, not when running, and when running Dll only

2.1.1.1 Add Lib

Method 1: Add the lib library by setting the project configuration.

A. Add the header file directory of the project: Project->Properties->Configuration Properties->c/c++->General->Additional Include Directory: Add the header file storage directory. Add header file reference 2.2.1.2 
B. Add the lib static library path referenced by the file: Project->Properties->Configuration Properties->Linker->General->Additional Library Directory: Add the lib file storage directory. 
C Then add the lib file name referenced by the project: Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies: Add the lib file name.

This method is cumbersome and unintuitive, and it may also require different configurations for the debug version and the release version, because the two versions of the library we generate may be placed in different directories.

Method 2: Use compile statement:

#ifdef _DEBUG
#pragma comment(lib,"..\\debug\\LedCtrlBoard.lib")
#else
#pragma comment(lib,"..\\release\\LedCtrlBoard.lib")
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

This method is intuitive and convenient, and can directly distinguish the different directories of the Debug version and the Release version as above. Of course, more versions can be distinguished through macros. However, when specifying the directory, it is easy to make mistakes accidentally.

Method 3: Add library files directly to the project.

Just like you added the .h and .cpp files, add the lib file to the project file list.

In VC, switch to "Solution View" -> select the project to add lib -> right click -> "Add" -> "Existing Item" -> select lib file -> OK.

This method is suitable for using the same lib library file in both the debug version and the Release version of my project. This saves you the tediousness of configuring the environment in method 1, and also saves the possibility of errors in method 2 statements. ..

2.1.1.2 Add header file

Before calling the function exported in the DLL, you need to include the corresponding header file. You can write an absolute path, or you can copy it to the same directory where the project calls the source file, or you can add (include) the header file directory through VS. The configuration method in VS: 
(1) VS Project->Right-click Properties->Configuration Properties->VC++ Directories->Include Directories 
(2) VS Project->Right-click Properties->Configuration Properties->C/C++->General->Additional Include Directories

2.1.1.3 Add dll

Generally, the dll can be copied to the runtime directory, which is in the same directory as the caller exe file. Of course, there are other ways to add the environment variable PATH, 
(1) VS project -> right-click properties -> configuration properties -> VC++ directory -> available Execution directory 
(2) Set the location of the DLL directory. The specific method is: right-click the project -> Properties -> Configuration Properties -> Debug -> Working Directory, you can set the path of the dll here.

Note 1: The difference between the release version and the debug version, the settings of each version are independent and should be set separately  
Note 2: There are the following methods for simply adding the lib directory

(1): Put the .lib file in the project directory where it is currently used; (eg: the directory where the .exe is located, or the directory where the project code is located) 
(2): For a certain project: "Project"->"Properties"- > "Configuration Properties" -> "VC++ Directories" -> "General" -> "Additional Library Directories" 
(3): In VS, "Project" -> "Properties" -> "Configuration Properties" -> "Linker" "->"General"->"Additional library directory" 
(4): Put it in the lib library directory of the development environment IDE, for example: "C:\Program Files\Microsoft Visual Studio 8\VC\lib", this is vs2005 The lib library directory developed by vc.

Note: When configuring the path in the VS properties, you can use an absolute path or a relative path, where ./ represents the current directory, and ../ represents the previous directory

The above is only valid for a single project. Those of us who have configured the opencv library know that there is a global configuration of lib and include header files, which is valid for all projects. Taking the Debug version as an example, the steps are as follows: 
(1) Click "View" → "Other Windows" → "Property Manager" 
(2) Open "Debug|Win32" → "Microsoft.Cpp.Win32.user" from the project on the left 
(3) Double-click "Microsoft.Cpp.Win32.user", in the pop-up window, click the VC++ directory on the left, edit the executable file directory, include directory and library directory on the right, add the corresponding paths respectively 
(4) Additional dependencies, click "Linker" → "Input" → "Additional Dependencies" Item", fill in the file name with the .lib suffix of the dependency.

2.1.2 Display connection

Although the implementation of implicit linking is relatively simple, in addition to the necessary .dll files, DLL .h files and .lib files are also required , which cannot be used in those occasions where only .dll files are provided, and only explicit linking can be used. . This method completes the loading and unloading of DLL by calling API functions, which can use memory more efficiently. This method is often used when writing large-scale applications. The specific implementation steps of this method are as follows:

①Use the Windows API function Load Library or AfxLoadLibrary provided by MFC to map the DLL module to the memory space of the process, and dynamically load the DLL module.

②Use the GetProcAddress function to get the pointer of the function to be called in the DLL.

③ When the DLL is not used, use the Free Library function or the AfxFreeLibrary function to explicitly unload the DLL from the address space of the process.

Using LoadLibrary to link explicitly, you can specify the full path to the DLL file in the function's parameters; if you don't specify a path, or perform an implicit link, Windows will follow the following search order to locate the search DLL:

  • Directory containing EXE files
  • Project directory
  • Windows system directory
  • Windows Directory
  • A sequence of directories listed in the Path environment variable

2.2 The use of static library lib

In static lib, a lib file is actually a collection of any obj files, and the obj files are generated by compiling cpp files. The .lib file of the static library contains all the information of the link library (function code and interface information). So when we call the static library .lib, we only need to include the header file directory (../include..h) and the additional library directory. Therefore, the use of the static link library requires the developer of the library to provide the .h header file and .lib file for the generated library

Create a new static library type project TestLib in VC, add the test.cpp file and the test.h file (the header file includes the function declaration), and then compile to generate the TestLib.lib file.

Other projects need to use this lib method:

(1) Add lib 
method 1): directly use "project right click" -> "add" -> "existing item" -> select lib file -> OK, in this way add .lib to the project 
method 2): Project Properties->Configuration Properties->Linker->Input->Additional Dependencies Add the name of the Lib library to be used; input in Project Properties->Configuration Properties->Linker->Input->Additional Library Directory. The path where the lib file is located (relative or absolute path)

Method 3): Or add the instruction #pragma comment(lib, "TestLib.lib") to the source code, or specify the full path (absolute path or relative path) #pragma comment(lib, "..\Debug\TestLib. lib"). The release or debug version of the lib can be distinguished by the macro #if defined(_DEBUG). In addition, if you do not specify the full path here, add additional library directories as in method 2.

If you do not add an additional lib library directory in the project properties, you can also copy the static library such as TestLib.lib to the directory where the project is located, or to the directory where the executable file is generated, or to the system Lib directory.

(2). Add header files

Add the corresponding header file test.h. #include "test.h"

The include file path can be an absolute path or a relative path relative to the directory where the project is located. If there are many header files, you can fill in the directory where your header files are located in the Additional include directories of project>settings>c/c++>preprocessor , which will automatically look for header files in this directory when compiling.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324504233&siteId=291194637