第五篇--VS2017如何生成Dll文件
参考资料: https://blog.csdn.net/qq_34097715/article/details/79540933 https://www.cnblogs.com/RascallySnake/p/3182807.html ? 生成Dll三步走 第一步:先建一个Dll项目 New --> Project --> Dynamic-Link Library(DLL) --> 取名,选路径 --> OK 第二步:编写头文件,例子是一个四则运算 selfTrainingDll.h #pragma once #ifdef DLL_TRAINING_API #else #define DLL_TRAINING_API _declspec(dllimport) //当编译时,头文件不参加编译,所以.cpp文件中先定义,后头文件被包含进来,因此外部使用时,为dllexport,而在内部编译时,则为dllimport #endif class DLL_TRAINING_API arithmetic_operation //需要被外界调用的类(父类) { public: double Add(double a,double b); double Sub(double a,double b); double Multi(double a,double b); double Div(double a,double b); }; int DLL_TRAINING_API export333(); ? 第三步:编写CPP文件,实现方法 selfTrainingDll.cpp // selfTrainingDll.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h" #define DLL_TRAINING_API _declspec(dllexport) #include <iostream> #include "selfTrainingDll.h" using namespace std; double DLL_TRAINING_API arithmetic_operation::Add(double a,double b) { return a+b; } double DLL_TRAINING_API arithmetic_operation::Sub(double a,double b) { return a - b; } double DLL_TRAINING_API arithmetic_operation::Multi(double a,double b) { return a * b; } double DLL_TRAINING_API arithmetic_operation::Div(double a,double b) { return a / b; } int DLL_TRAINING_API export333() { return 333; } ? 第三步:生成Dll Build --> Build Solution 至此,文件生成完毕 ? 静态方法调用Dll文件 第一步:创建一个控制台程序 省略 第二步:编译运行,产生Debug文件夹 第三步:将之前Dll项目生成的selfTrainingDll.h和selfTrainingDll.lib放入项目文件夹下,将selfTrainingDll.dll放入Debug文件夹下 第四步:在项目中添加selfTrainingDll.h头文件 第五步:在Cpp中调用Dll UseSelfDll.cpp // UseSelfDll.cpp : This file contains the ‘main‘ function. Program execution begins and ends there. // #include "pch.h" #include <iostream> using namespace std; #include "selfTrainingDll.h" #pragma comment(lib,"selfTrainingDll.lib") int main() { arithmetic_operation ao; cout << ao.Add(1,2) << endl; cout << ao.Sub(2,1) << endl; cout << ao.Multi(2,1) << endl; cout << ao.Div(6,4) << endl; cout << export333() << endl; cout << "Hello World!n"; } ? 至此,调用成功 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows-server-2012 – 在服务器核心上自动使用PowerShell
- 授予用户在Windows Server 2012域控制器上更改系统时间的权
- 如何检测壁纸何时发生变化(Windows XP或更高版本)?
- Windows 8中的视觉样式
- Windows安装程序 – 启动条件以检测Office 2010应用程序
- silent-installer – 如何在没有用户交互的情况下安装Micro
- .net – 如何保持ReadDirectoryChangesW缺少文件更改
- 获取Microsoft 10 Edge浏览器Mime类型php
- Windows XP全盘加密 – 有哪些选择?
- 如何在Windows上将Ruby添加到PATH变量中?