1. Implicit link (. dll,。 lib,。 H required)
2. Explicit link (. dll,。 H required)
Method 1: implicit link-requirement. lib,。 dll,。 H file.
Implicit link is to load the DLL file into memory when the program starts to execute, while display link is loaded in real time, loaded when the program needs it, and unloaded when it doesn't.
This method requires DLL files, as well as corresponding Lib files and header files.
As long as there is no explicit link in the program, it is implicit link.
The bin directory of Windows programs contains executable files (. Exe) and dynamic link library (. Dll), lib directory contains static libraries.
step
Step one: put. Dll, lib and H file into the corresponding search path.
Click here to check the search path of dynamic library, and remember the two most important ones.
1, the current directory of the project (. Cpp) directory
2. Directory in the 2.path environment variable
Low static library search path includes
1, the current directory of the project. Cpp directory (debugging of projects and solutions is not available) (it is not a solution directory either).
2. Library directory in 2.VC settings.
Note: If the lib library is not placed in the search path, it can also be added to the program.
# pragma comment (lib, "d:/dlltest. lib))// If it is a relative path, it is the current path. The cpp directory of the project.
● The header file search path includes
1, including directory (including? Directory)
Step 2: Be sure to add the name of the library you use to the dependency in VC settings.
The first type (corresponding to the above search paths 1): for a small number of libraries.
Settings. dll,。 Lib search path: put it directly. dll,。 lib,。 H into the current directory of the project (that is, the directory containing the source files of the project. cpp)。
Set the search path. H: include directory (including? Directory) to add your. H path.
Then in the link-> Input->; Add the static library you want to use to the additional dependency.
Only static libraries have been added here.
You can use the functions in the DLL.
The second type (corresponding to each search path 2 above): for a relatively large number of libraries (Opencv).
Configuration of open source libraries such as OpenCV and OSG.
This method is also a common configuration method of open source libraries, such as Opencv and OSG.
Steps are as follows
Step 1. Add the directory of the DLL.
Method 1: ★ Put the dll directory (usually the bin directory) into the environment variable path (which needs to be restarted), and Path is the directory for searching the dynamic library.
Step two. Add static librarylib library directory and header file directory.
Add the static library directory and the header file directory to the VC++ directory in the project.
Step three. Add static library
On the link-> Input->; Add the static library you want to use to the additional dependency (when calling in the program, you will look up this library in the library directory configured above).
Only the static library is added here, and the directory of the dynamic library has been put into the environment variable. The path will be searched and automatically loaded. dll。
Such as rmwroadboundarytrealtlinedell.lib.
Then, add a header file (found in the header file directory configured above) to the program, and you can use the functions in the DLL.
Method 2: Explicit Link-Requires a DLL (without. lib. H file).
Is to explicitly load the DLL library.
Note: when displaying links and exporting DLLs, you should adopt the way of Extern "c" instead of _declspec(dllexport), because _declspec(dllexport) will have the problem of "name adaptation" (using _cdecl to call the C++ compilation method of the specification).
Example:
For example, the header file corresponding to the library file is as follows
#ifndef DLLTEST_H
# defined test _H
//This macro is exported using _ _ declspec (dllExport) in the DLL project.
//When used outside a dll project, use __declspec(dllimport) to import it.
//The macro DLL_EXPORTS is in the. cpp。
#ifdefDLL_EXPORTS
#defineDLL_EXPORTS? extern"C"_declspec(dllexport)
# Otherwise
# define DLL_EXPORTS? extern"C"_declspec(dllimport)
#endif
//Function declaration
DLL_EXPORTSint? Add(int a,int b);
DLL_EXPORTSint? Sub(int a,int b);
DLL_EXPORTSint? Divide(int a,int b);
#endif//DLLTEST_H
Test code:
typedefint(*Add)(int a,int b);
typedefinit(* Sub)(int a,int b);
HINSTANCE hDLL
Add Add _; //Function pointer
hDLL = LoadLibrary(_ T(" D:/dlltest . dll ")); //Load the dynamic link library DLLTest.dll file;
Add_ = (Add)GetProcAddress(hDLL," Add ");
intresult = Add_(5,8);
printf("5+8:%d\n ",result);
Free Library (HDLL); //uninstall. Dll file;
The following contents are exported by __declspec(dllexport), which is not recommended. Please pay attention to the spelling of the function name!
Now there is a function in DLLTest.h (derived from __declspec(dllexport)).
#ifndef DLLTEST_H
# defined test _H
//This macro is exported using _ _ declspec (dllExport) in the DLL project.
//When used outside a dll project, use __declspec(dllimport) to import it.
//The macro DLL_EXPORTS is in the. cpp。
#ifdefDLL_EXPORTS
#defineDLL_EXPORTS? __declspec(dllexport)
# Otherwise
# define DLL_EXPORTS? __declspec(dllimport)
#endif
intDLL_EXPORTS Add(int a,int b);
intDLL_EXPORTS Sub(int a,int b);
intDLL_EXPORTS Divide(int a,int b);
#endif//DLLTEST_H
Test code
typedefint(*Add)(int a,int b);
typedefinit(* Sub)(int a,int b);
HINSTANCE hDLL
Add Add _; //Function pointer
//You can also use hdll = loadlibrary (_ t ("d:/dlltest.dll"));
hDLL = loadlibrary a((" D:/dlltest . dll ")); //Load the dynamic link library DLLTest.dll file;
Add_ = (Add)GetProcAddress(hDLL,"? Add @ @ yahhh @ z "); //! ! ! ! Get function address
intresult = Add_(5,8);
printf("5+8:%d\n ",result);
Free Library (HDLL); //uninstall. Dll file;
What should be noted here is: GetProcAddress(hDLL, "function name"); The function name in is the function name in DLL, which can be viewed by PE Explorer software.
Because VC++ is used to handle function names, so
GetProcAddress(hDLL,"? Add @ @ yahhh @ z "); // GetProcAddress(hDLL, "function name");
Instead of simply "adding", because the function name in the DLL is a function name that has been processed by VC++.
Display the call, preferably export the DLL in the form of extern "c".
Therefore, when using the implicit link mode, only the required DLL is loaded, and in the additional dependencies, only the required DLL is added to the lib, not more, otherwise it will cause 1. Increase program startup time. 2. Memory waste.