1. What is a static connection library LIB and what is a dynamic link library DLL? Both static and dynamic link libraries share code, both in binary format
2. The difference between static link library LIB and dynamic link library DLL If you use a statically linked library, all the instructions in the lib are directly included in the resulting EXE file. The dynamic dynamic link library does not have to be included in the final EXE file, which can be "dynamically" referenced and unloaded when the EXE file is executed. Another difference is that a static link library can no longer contain other dynamic or static link libraries, while a dynamic link library can contain other dynamic or static link libraries.
3. Static link library call method: Let's survive to generate a lib file:
VS2017--> Create a new project-->windows desktop--> static library, the project name is Libtest
New functem.h and functem.cpp
functem.h:
#pragma once #include "stdafx.h" #ifndef _functem_H #define _functem_H int functem(int a, int b); #endif
functem.cpp:
#include "functem.h" #include "stdafx.h"
int functem(int a, int b)
{ return a + b;
} Then generate the project, and there will be two files, Libtest.lib and Libtest.pdb, in the Debug directory
When calling, three steps are required:
1. Copy Libtest.lib and functem.h to the directory of the caller; as shown in the figure below:
2. Add #include "functem.h" above the calling cpp file
3. Add #pragma comment(lib, "Libtest.lib") above the calling cpp file
As shown in the figure above, the call has been successful.
4. Dynamic link library call method: Create a new project - > dynamic link library with the project name Dlltest
Create a new Dlltest.h:
#pragma once #ifndef __CDLL_H__ #define __CDLL_H__ extern "C" int _declspec(dllexport) add(int x, int y); #endif
Dlltest.cpp:
#include "stdafx.h" #include "Dlltest.h" int add(int x, int y)
{ return x + y;
} Generate the project, in the Debug directory there is:
Calling process:
1. Copy Dlltest.dll, Dlltest.lib and Dlltest.h and copy them to the directory of the caller; as shown in the figure below:
PS: Here, in fact, Dlltest.lib is only used when compiling the compiler link, so after the program is generated, you can not include the lib file, only the dll file, and the real use is actually the dll file. In short, all three files need to be copied, but .h and .lib are used to compile links, and only .dll are required to be placed in the final generated directory.
2. Add #include "Dlltest.h" above the called cpp file
3. Add #pragma comment(lib, "Dlltest.lib") above the calling cpp file
The call is successful! --------------------- Author: Bird Source: CSDN Original: https://blog.csdn.net/wcc27857285/article/details/84615891 Copyright Notice: This article is an original article by the blogger, please attach the blog post link for reprinting!
|