1. Static Link Library
Create a "Win32 project" and select Lib in "application settings. Create the Lib. h and Lib. cpp files. The source code of Lib. h and Lib. cpp is as follows:
// File: Lib. h # ifndef lib_h # define lib_hextern "C" int add (int x, int y); // declare as an external function of C compilation and connection mode # endif // file: Lib. CPP # include "Lib. H "int add (int x, int y) {return X + Y ;}
After compilation, the DLL and Lib files are generated. The following describes how to use them. The source code is as follows:
# Include <stdio. h> # include ".. \ Lib. H "# pragma comment (Lib ,".. \ debug \ libtest. lib ") // specify to connect int main (INT argc, char * argv []) {printf (" 2 + 3 = % d ", add (2, 3 ));}
2. Export Functions
Create a Win32 project and select DLL in application settings. Create two files: commendll. h and commendll. cpp. In commendll. h
namespace CommenDLL{ class MathFun { public: static __declspec(dllexport)double Add(double a, double b); static __declspec(dllexport)double Minus(double a, double b); };}
In commendll. cpp
namespace CommenDLL{ double MathFun::Add(double a, double b) { return a+b; } double MathFun::Minus(double a, double b) { return a-b; }}
3. Export class
In the header file of the same project
class __declspec(dllexport) Circle{public: Circle(){}; ~Circle(){}; void SetCenter(Point point); void SetRadius(int r); double GetZhouChang(); double GetMianJi();private: Point center; int radius;};
CPP File
void Circle::SetCenter(Point point){ this->center.x = point.x; this->center.y = point.y;}void Circle::SetRadius(int r){ this->radius = r;}double Circle::GetZhouChang(){ const double pi = 3.1415926; return this->radius*pi*2;}double Circle::GetMianJi(){ const double pi = 3.1415926; return pi*this->radius*this->radius;}
When calling, import the generated library and directly use its functions or classes.