wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了第五篇--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) --> 取名,选路径

资料:

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

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

#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

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

// 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

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

// 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

 

至此,调用成功

大佬总结

以上是大佬教程为你收集整理的第五篇--VS2017如何生成Dll文件全部内容,希望文章能够帮你解决第五篇--VS2017如何生成Dll文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。