Static Link Libraries and Dynamic Link Libraries

Static Link Libraries and Dynamic Link Libraries

There are two kinds of libraries:

One is that the LIB contains the information (entry) of the DLL file where the function is located and the function location in the file. The code is provided by the DLL loaded in the process space at runtime, which is called the dynamic link library.

One is that the LIB contains the function code itself, and the code is directly added to the program at compile time, which is called the static link library.

There are two ways to link:

Dynamic linking uses a dynamic link library that allows an executable module (.dll file or .exe file) to contain only the information needed to locate the executable code of the DLL function at runtime.

Static linking uses a static link library, the linker gets all the referenced functions from the static link library LIB and puts the library together with the code into the executable file.

The difference between lib and dll is as follows:

(1) lib is used at compile time, and dll is used at runtime. If you want to compile the source code, you only need lib; if you want to run the dynamically linked program, you only need dll.

(2) If there is a dll file, then lib is generally some index information, which records the entry and location of the function in the dll, and the specific content of the function in the dll; if there is only a lib file, then the lib file is statically compiled, and the index and implementation are in it. Using statically compiled lib files, you do not need to hang the dynamic library when running the program. The disadvantage is that the application is relatively large, and the flexibility of the dynamic library is lost. When a new version is released, a new application must be released.

(3) In the case of dynamic linking, there are two files: one is the LIB file and the other is the DLL file. The LIB contains the names and locations of the functions exported by the DLL, the DLL contains the actual functions and data, and the application uses the LIB file to link to the DLL file. In the executable file of the application, instead of the called function code, the address of the corresponding function code in the DLL is stored, thereby saving memory resources. DLL and LIB files must be distributed with the application, otherwise the application will generate errors. If you don't want to use the lib file or there is no lib file, you can use the WIN32 API functions LoadLibrary and GetProcAddress to load.

Compile and generate LIB :

Create a win32 console program and check the LIB type. Add and write apps, generate solutions

1 >------ Build started: Project: test, Configuration: Debug Win32 ------
 1 >   test.cpp
 1 > test.vcxproj -> D:\visual studio\test\Debug\test. lib
 ========== Build: 1 succeeded, 0 failed , 0 latest , 0 skipped ==========

Compile to generate DLL:

Create a win32 console program, check the DLL type, and create add.h

#ifndef __ADD_H__
#define __ADD_H__
extern "C" int _declspec(dllexport) add(int x, int y);
#endif

Note here: Use the _declspec(dllexport) keyword to export data, functions, classes or class member functions from a DLL. _declspec(dllexport) will add the export instruction to the object file. If dllexport is not applicable, the function cannot be found when the dll is used dynamically

Create add.cpp

#include "add.h"
int add(int a, int b)
{
    return a + b;
}

Then click Generate---Generate solution, a DLL file (add.dll) will be generated in the Debug directory of the project

1> add.vcxproj -> D:\visual studio\add\Debug\add.dll
Dynamically call DLL (only DLL is required, no header files and LIBs are required)
#include <iostream>
#include <windows.h>
using namespace std;
typedef int (*FUN)( int , int ); // Define a pointer to the same function prototype as in the DLL 
int main()
{
    int x, y;
    HMODULE hDLL = LoadLibrary("D:\\visual studio\\add\\Debug\\add.dll");//加载dll
    if (hDLL != NULL)
    {
        FUN add = FUN(GetProcAddress(hDLL, " add " )); // Get the function pointer imported into the application, and get 
        if (add != NULL) according to the method name
        {
            cout << "Input 2 Numbers:";
            cin >> x >> y;
            cout << add(x, y) <<endl;
        }
        else
        {
            cout << "Cannot Find Function"  << endl;
        }
        FreeLibrary(hDLL);
    }
    else
    {
        cout << "Cannot Find dll"  << endl;
    }
    return 1;
}

Generate result:

Input 2 Numbers: 3  4 
7 
Press any key to continue . . .

Static call (requires header files, LIB and DLL files at the same time, all of them are indispensable)

1: Copy the dll and lib under the dll project to the debug folder in the test project

2: Write the header file of the test project

#ifndef __USEDll_H__
#define __USEDll_H__
extern "C" _declspec(dllimport) int add(int x, int y);
#endif

3: Write cpp file

#include "UseDll.h"
#include <iostream>
using namespace std;
#pragma comment(lib,"D:\\visual studio\\UseDll\\Debug\\add.lib")
int main()
{
    int x, y;
    cout << "Input 2 Numbers:";
    cin >> x >> y;
    cout << add(x, y) <<endl;
}

The results are as follows:

Input 2 Numbers:10 10
20
Please press any key to continue . . .

Description: #pragma comment (lib,"...lib") is to tell the linker to link the lib file first. In addition to using this statement, you can go to project properties --> linker --> input --> additional dependencies add this lib. At this point, the lib needs to be placed in the system path.

Note here that the lib here is generated when the dll is generated, which is smaller than the lib generated when the lib is generated. In fact, this is an import library, not a static library. The static library itself contains the actual execution code, symbol table, etc. For the import library, its actual execution code is located in the dynamic library, and the import library only contains the address symbol table, the address of each function, etc., to ensure that the program finds some basic address information of the corresponding function.

 

Reference document: https://blog.csdn.net/red10057/article/details/7394213

https://blog.csdn.net/Sya_inn/article/details/53981440

http://www.cppblog.com/amazon/archive/2009/09/04/95318.html

 

Guess you like

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