there are many advantages to use the dynamic link library when we develop the software by C plus plus, here i just say something about how to generate a dll and how to use it in a project. c++
Though windows via c/c++ and vc++ deeply programming by sun xin had describe more about this, here I just want to highlight one point. Meanwhile, I will use the exmple of the book written by Sunxin. windows
1, create the Dill project: ui
Dill.h file: this
#ifdef DLL1_API spa
#else orm
#define DLL1_API extern "C" _declspec (dllimport) it
#endif io
DLL1_API int add(int a, int b); test
DLL1_API int substract(int a, int b); import
2, create dill.cpp :
#define DLL1_API extern "C" _declspec (dllexport)
#include "Dill.h"
int add(int a, int b)
{
return a + b;
}
int substract( int a, int b)
{
return a - b;
}
3, create dill test project with MFC fram
HINSTANCE hInst;
hInst = LoadLibrary("Dill.dll");
typedef int (*ADDPROC)(int a, int b);
ADDPROC Add = (ADDPROC)GetProcAddress(hInst, "add");
if(!Add)
{
MessageBox("Error!");
return;
}
CString str;
str.Format("5+3= %d", Add(5,3));
MessageBox(str);
4,move the Dill.h file to the dilltest project.
5, move the dill.dll and dill.lib file to the dilltest project.
5,build the dilltest project, if no exception, I think the build can pass.