C++ DLL库实现

1. 导出函数  

mathfun.h

  1. #ifndef MATHFUN_DEF  
  2. #define MATHFUN_DEF  
  3.   
  4. #ifdef OPENMVGDLL_EXPORTS  
  5. #define MATHFUN_API __declspec(dllexport)   
  6. #else  
  7. #define MATHFUN_API __declspec(dllimport)   
  8. #endif  
  9.   
  10. extern "C"  
  11. {  
  12.     int MATHFUN_API m_add(int a, int b);  
  13.   
  14.     int MATHFUN_API m_sub(int a, int b);  
  15.   
  16. }  
  17.   
  18. #endif // !MATHFUN_DEF


mathfun.cpp

  1. #include "mathfun.h"  
  2.   
  3. #define OPENMVGDLL_EXPORTS  
  4.   
  5. int m_add(int a, int b)  
  6. {  
  7.     return (a + b);  
  8. }  
  9.   
  10. int m_sub(int a, int b)  
  11. {  
  12.     return (a - b);  
  13. }


2. 导出类


导出函数类似,把导出宏加到类前(eg. class MATHFUN_API myclass )

并在类函数前加 MATHFUN_API

导出类不需要 extern "C"


  1. #ifndef MATHFUN_DEF  
  2. #define MATHFUN_DEF  
  3.   
  4. #ifdef OPENMVGDLL_EXPORTS  
  5. #define MATHFUN_API __declspec(dllexport)   
  6. #else  
  7. #define MATHFUN_API __declspec(dllimport)   
  8. #endif  
  9.   
  10. class MATHFUN_API myClass  
  11. {  
  12.     int MATHFUN_API m_add(int a, int b);  
  13.   
  14.     int MATHFUN_API m_sub(int a, int b);  
  15.   
  16. }  
  17.   
  18. #endif // !MATHFUN_DEF

3. 调用DLL库


添加 mathfun.h mathfun.lib mathfun.dll到当前工程目录下

调用文件头部添加 

#define OPENMVGDLL_EXPORTS

#include "mathfun.h"

然后就可直接调用函数和类(注意顺序不能变)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值