How to create and invoke C++ DLL dynamic Library
This article introduces the knowledge of "how to create and invoke C++ DLL dynamic library". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Create a library project
2. Add header files
ClassDll.h
/ / Macro definitions prevent repeated compilation of .h files # ifndef _ DLLCLASS_H#define _ DLLCLASS_H// dll library files define macros (DLLCLASS_EXPORTS) use _ declspec (dllexport) / / when using dll library files _ declspec (dllimport) (without defining macros) # ifdef DLLCLASS_EXPORTS#define EXT_CLASS _ declspec (dllexport) # else#define EXT_CLASS _ declspec (dllimport) # endif// defines the class of the library file (export or import) class EXT_CLASS CMath {public: / / define the function int Add (int item1 Int item2) Int Sub (int item1, int item2);}; # endif3, add cpp file
ClassDll.cpp
/ / define the DLLCLASS_EXPORTS header file class / / use _ declspec (dllexport) to export # define DLLCLASS_EXPORTS#include "ClassDll.h" / / implement the class function int CMath::Add (int item1, int item2) {return item1 + item2;} int CMath::Sub (int item1, int item2) {return item1-item2;} 4, compile the dll project
Generate a file
5. Create a calling project
General engineering, multi-byte project
6. Call the project to add the cpp file
UseClassdll.cpp
# include using namespace std;// import header file library class uses _ declspec (dllimport) to export class # include ".. / ClassDll/ClassDll.h" / / implicitly call dll load library file # pragma comment (lib, ".. / Debug/ClassDll.lib") / / Runtime dll file and exe file in one folder int main () {/ / define class CMath math in dll library / / call the function int sum = math.Add (5,6); int sub = math.Sub (5,6); / / print the result cout