C++ DLL 模板
1、使用VS2005创建Win32 DLL项目,选择空项目,然后加入CppDll.h和CppDll.cpp文件。
2、修改CppDll.h和CppDll.cpp文件使之成为需要的内容。
3、编译生成CppDll.dll。
下面是模板文件:
- //
- // 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__
- //
- // 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;
- }
分类:
C++/C
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!