C++ DLL 模板 .

简介:  C++ DLL 模板 1、使用VS2005创建Win32 DLL项目,选择空项目,然后加入CppDll.h和CppDll.cpp文件。 2、修改CppDll.h和CppDll.cpp文件使之成为需要的内容。

 C++ DLL 模板

1、使用VS2005创建Win32 DLL项目,选择空项目,然后加入CppDll.h和CppDll.cpp文件。

2、修改CppDll.h和CppDll.cpp文件使之成为需要的内容。

3、编译生成CppDll.dll。

下面是模板文件:

  1. //   
  2. // CppDll.h   
  3. // by cheungmine   
  4. // C++ DLL 模板   
  5. //   
  6. /*** 使用CPPDLL: 
  7. #include "../CppDll.h" 
  8. #ifdef _DEBUG 
  9. #  pragma comment(lib, "F:/del/CppDll/Debug/CppDlld.lib") 
  10. #else 
  11. #  pragma comment(lib, "F:/del/CppDll/Release/CppDll.lib") 
  12. #endif 
  13. ***/  
  14. #ifndef _CPPDLL_H__   
  15. #define _CPPDLL_H__   
  16. //#include <windows.h>   
  17. //#include <math.h>   
  18. //#include <assert.h>   
  19. //#include <memory.h>   
  20. //#include <malloc.h>   
  21. // 下列 ifdef 块是创建使从 DLL 导出更简单的宏的标准方法。   
  22. // 此 DLL 中的所有文件都是用命令行上定义的 CPPDLL_EXPORTS 符号编译的。   
  23. // 在使用此 DLL 的任何其他项目上不应定义此符号。   
  24. // 这样,源文件中包含此文件的任何其他项目都会将 CPPDLL_API 函数视为是从此 DLL 导入的,   
  25. // 而此 DLL 则将用此宏定义的符号视为是被导出的。   
  26. #ifdef CPPDLL_EXPORTS   
  27. #define CPPDLL_API __declspec(dllexport)   
  28. #else   
  29. #define CPPDLL_API __declspec(dllimport)   
  30. #endif   
  31. #define     CPPDLL_VERSION   1.0            // 常量定义   
  32. // 名称空间   
  33. namespace CppDll  
  34. {  
  35. //   
  36. // 从 CppDll.dll 导出类   
  37. //   
  38. // 导出类: MyStruct   
  39. struct CPPDLL_API MyStruct  
  40. {  
  41.     long    x;  
  42.     long    y;  
  43. };  
  44. // 导出类: MyClass2   
  45. class CPPDLL_API MyClass2  
  46. {  
  47.     void Clear()  
  48.     {  
  49.         // 实现   
  50.     };  
  51. public:  
  52.     MyClass2();  
  53.     ~MyClass2();  
  54. };  
  55. // 导出共享变量   
  56. extern CPPDLL_API int g_nVar;  
  57. //   
  58. // 导出方法   
  59. //   
  60. CPPDLL_API double Method(const MyStruct *s1, const MyStruct *s2);  
  61. CPPDLL_API double Method(const MyStruct &s1, const MyStruct &s2);  
  62.   
  63. };   // End of namespace CppDll   
  64. #endif  // _CPPDLL_H__  
// // CppDll.h // by cheungmine // C++ DLL 模板 // /*** 使用CPPDLL: #include "../CppDll.h" #ifdef _DEBUG # pragma comment(lib, "F:/del/CppDll/Debug/CppDlld.lib") #else # pragma comment(lib, "F:/del/CppDll/Release/CppDll.lib") #endif ***/ #ifndef _CPPDLL_H__ #define _CPPDLL_H__ //#include <windows.h> //#include <math.h> //#include <assert.h> //#include <memory.h> //#include <malloc.h> // 下列 ifdef 块是创建使从 DLL 导出更简单的宏的标准方法。 // 此 DLL 中的所有文件都是用命令行上定义的 CPPDLL_EXPORTS 符号编译的。 // 在使用此 DLL 的任何其他项目上不应定义此符号。 // 这样,源文件中包含此文件的任何其他项目都会将 CPPDLL_API 函数视为是从此 DLL 导入的, // 而此 DLL 则将用此宏定义的符号视为是被导出的。 #ifdef CPPDLL_EXPORTS #define CPPDLL_API __declspec(dllexport) #else #define CPPDLL_API __declspec(dllimport) #endif #define CPPDLL_VERSION 1.0 // 常量定义 // 名称空间 namespace CppDll { // // 从 CppDll.dll 导出类 // // 导出类: MyStruct struct CPPDLL_API MyStruct { long x; long y; }; // 导出类: MyClass2 class CPPDLL_API MyClass2 { void Clear() { // 实现 }; public: MyClass2(); ~MyClass2(); }; // 导出共享变量 extern CPPDLL_API int g_nVar; // // 导出方法 // CPPDLL_API double Method(const MyStruct *s1, const MyStruct *s2); CPPDLL_API double Method(const MyStruct &s1, const MyStruct &s2); }; // End of namespace CppDll #endif // _CPPDLL_H__

 

 

  1. //   
  2. // CppDll.cpp   
  3. // by cheungmine   
  4. //   
  5. #include "CppDll.h"   
  6. // 包含其他必要文件   
  7. // #include <vector>   
  8. using namespace CppDll;  
  9. ///////////////////////////////////////////////////////////////////////////////   
  10. // struct MyStruct   
  11.   
  12.   
  13. ///////////////////////////////////////////////////////////////////////////////   
  14. // class MyClass2   
  15. MyClass2::MyClass2()  
  16. {  
  17. }  
  18.   
  19. MyClass2::~MyClass2()  
  20. {  
  21. }  
  22. ///////////////////////////////////////////////////////////////////////////////   
  23. // 导出变量   
  24. CPPDLL_API int g_nVar = 0;  
  25. ///////////////////////////////////////////////////////////////////////////////   
  26. // 导出方法   
  27. CPPDLL_API double CppDll::Method(const MyStruct *s1, const MyStruct *s2)  
  28. {  
  29.     return 0;  
  30. }  
  31. CPPDLL_API double CppDll::Method(const MyStruct &s1, const MyStruct &s2)  
  32. {  
  33.     return 0;  
  34. }  
// // CppDll.cpp // by cheungmine // #include "CppDll.h" // 包含其他必要文件 // #include <vector> using namespace CppDll; /////////////////////////////////////////////////////////////////////////////// // struct MyStruct /////////////////////////////////////////////////////////////////////////////// // class MyClass2 MyClass2::MyClass2() { } MyClass2::~MyClass2() { } /////////////////////////////////////////////////////////////////////////////// // 导出变量 CPPDLL_API int g_nVar = 0; /////////////////////////////////////////////////////////////////////////////// // 导出方法 CPPDLL_API double CppDll::Method(const MyStruct *s1, const MyStruct *s2) { return 0; } CPPDLL_API double CppDll::Method(const MyStruct &s1, const MyStruct &s2) { return 0; }

 

目录
相关文章
|
12天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
17 2
|
22天前
|
编译器 C++
C++入门指南:10分钟带你快速了解模板究竟是什么(建议收藏!!)
C++入门指南:10分钟带你快速了解模板究竟是什么(建议收藏!!)
27 0
|
25天前
|
安全 算法 编译器
【C++ 泛型编程 进阶篇】深入探究C++模板参数推导:从基础到高级
【C++ 泛型编程 进阶篇】深入探究C++模板参数推导:从基础到高级
240 3
|
25天前
|
存储 算法 编译器
【C++ TypeName用法 】掌握C++中的TypeName:模板编程的瑞士军刀
【C++ TypeName用法 】掌握C++中的TypeName:模板编程的瑞士军刀
234 0
|
25天前
|
安全 算法 C++
【C++泛型编程 进阶篇】模板返回值的优雅处理(二)
【C++泛型编程 进阶篇】模板返回值的优雅处理
31 0
|
25天前
|
安全 算法 编译器
【C++泛型编程 进阶篇】模板返回值的优雅处理(一)
【C++泛型编程 进阶篇】模板返回值的优雅处理
42 0
|
25天前
|
存储 算法 编译器
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
47 1
|
25天前
|
机器学习/深度学习 算法 编译器
【C++ 泛型编程 中级篇】深度解析C++:类型模板参数与非类型模板参数
【C++ 泛型编程 中级篇】深度解析C++:类型模板参数与非类型模板参数
46 0
|
25天前
|
设计模式 程序员 C++
【C++ 泛型编程 高级篇】C++模板元编程:使用模板特化 灵活提取嵌套类型与多容器兼容性
【C++ 泛型编程 高级篇】C++模板元编程:使用模板特化 灵活提取嵌套类型与多容器兼容性
241 2
|
25天前
|
设计模式 算法 数据安全/隐私保护
【C++ 引用 】C++深度解析:引用成员变量的初始化及其在模板编程中的应用(二)
【C++ 引用 】C++深度解析:引用成员变量的初始化及其在模板编程中的应用
25 0
【C++ 引用 】C++深度解析:引用成员变量的初始化及其在模板编程中的应用(二)