当前位置:网站首页>dllexport和dllimport
dllexport和dllimport
2022-07-03 14:33:00 【vegetablesssss】
在VS中,如果要跨项目使用类或变量,就必须用到dllexport和dllimport,下面分别举例全局变量,函数和类跨项目使用。
环境准备:使用VS分别新建一个windows应用程序和DLL项目,windows应用程序项目名称为ConsoleApplication1,DLL项目名称为testExtern。

在ConsoleApplication1上右键项目属性
附加库目录加上testExtern生成dll和lib所在的目录
附加依赖项加上testExtern.lib名称
全局变量
在同一个项目中,全局变量不需要导出,extern声明一下即可:
test.cpp中定义:
int a = 100;
main.cpp中使用:
extern int a;
int main()
{
cout << a << endl;
}
输出100;
在textExtern项目中定义全局变量(只有定义了导出宏时才会生成lib文件),则需要将其导出:
int _declspec(dllexport) sssss = 1000000;
在ConsoleApplication1中使用sssss这个全局变量时,则需要导入:
extern int _declspec(dllimport) sssss;
int main()
{
cout << sssss << endl;
return 0;
}
函数
一定要在函数定义时声明导出:
void _declspec(dllexport) myFun()
{
std::cout << "myFun()" << std::endl;
}
在main.cpp中包含声明的头文件,不包含头文件则需要extern并声明导入
int main()
{
myFun();
return 0;
}
类
在testExtern项目中ExportClass必须在定义时导出,在ConsoleApplication1中使用时必须包含其头文件
class _declspec(dllexport) ExportClass
{
public:
void testExportClass();
};
void ExportClass::testExportClass()
{
std::cout << "textExportClass()" << std::endl;
}
main.cpp中:
#include "C:\Users\Administrator\Documents\Visual Studio 2015\Projects\ConsoleApplication1\testExtern\extern.h"
int main()
{
ExportClass ex;
ex.testExportClass();
return 0;
}
边栏推荐
猜你喜欢

tonybot 人形机器人 首次开机 0630

Paper sharing: generating playful palettes from images

US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars

Exercise 10-2 recursive factorial sum
![洛谷P4047 [JSOI2010]部落划分 题解](/img/7f/3fab3e94abef3da1f5652db35361df.png)
洛谷P4047 [JSOI2010]部落划分 题解

7-18 finding the single root of polynomial by dichotomy

tonybot 人形机器人 红外遥控玩法 0630

Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things

【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?

Sword finger offer 28 Symmetric binary tree
随机推荐
MongoDB数据库入门的常用命令
String sort
Niuke: crossing the river
tonybot 人形机器人 定距移动 代码编写玩法
洛谷P5536 【XR-3】核心城市 题解
关于敏捷的一些概念
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
Accelerating strategy learning using parallel differentiable simulation
tonybot 人形机器人 红外遥控玩法 0630
愉悦资本新双币基金近40亿元完成首次关账
Preliminary summary of structure
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
MySQL multi table query subquery
Selective sorting
Creation of data table of Doris' learning notes
常见问题之PHP——ldap_add(): Add: Undefined attribute type in
Sword finger offer 28 Symmetric binary tree
Exercise 8-2 calculate the sum and difference of two numbers
Exercise 10-2 recursive factorial sum
Why is this error reported when modifying records in the database