加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

第五篇--VS2017如何生成Dll文件

发布时间:2020-12-14 02:02:14 所属栏目:Windows 来源:网络整理
导读:参考资料: 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 第二步:编

参考资料:

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();
View Code

?

第三步:编写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;
}
View Code

?

第三步:生成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"; 
}
View Code

?

至此,调用成功

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读