1. 导出函数
mathfun.h
- #ifndef MATHFUN_DEF
- #define MATHFUN_DEF
- #ifdef OPENMVGDLL_EXPORTS
- #define MATHFUN_API __declspec(dllexport)
- #else
- #define MATHFUN_API __declspec(dllimport)
- #endif
- extern "C"
- {
- int MATHFUN_API m_add(int a, int b);
- int MATHFUN_API m_sub(int a, int b);
- }
- #endif // !MATHFUN_DEF
- #include "mathfun.h"
- #define OPENMVGDLL_EXPORTS
- int m_add(int a, int b)
- {
- return (a + b);
- }
- int m_sub(int a, int b)
- {
- return (a - b);
- }
2. 导出类
与导出函数类似,把导出宏加到类前(eg. class MATHFUN_API myclass )
并在类函数前加 MATHFUN_API
导出类不需要 extern "C"
- #ifndef MATHFUN_DEF
- #define MATHFUN_DEF
- #ifdef OPENMVGDLL_EXPORTS
- #define MATHFUN_API __declspec(dllexport)
- #else
- #define MATHFUN_API __declspec(dllimport)
- #endif
- class MATHFUN_API myClass
- {
- int MATHFUN_API m_add(int a, int b);
- int MATHFUN_API m_sub(int a, int b);
- }
- #endif // !MATHFUN_DEF
3. 调用DLL库
添加 mathfun.h mathfun.lib mathfun.dll到当前工程目录下
调用文件头部添加
#define OPENMVGDLL_EXPORTS
#include "mathfun.h"
然后就可直接调用函数和类(注意顺序不能变)