当前位置:网站首页>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;
}
边栏推荐
- ConstraintLayout 的使用
- X86 assembly language - Notes from real mode to protected mode
- Understand the application scenario and implementation mechanism of differential segment
- Solr series of full-text search engines - basic principles of full-text search
- C library function - qsort()
- Table of mathematical constants by q779
- pyQt界面制作(登录+跳转页面)
- Paper sharing: generating playful palettes from images
- Convert string to decimal integer
- Mysql多表查询 #子查询
猜你喜欢
编程语言:类型系统的本质
分布式事务(Seata) 四大模式详解
556. The next larger element III
Doris学习笔记之数据表的创建
Use of constraintlayout
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
Exercise 10-3 recursive implementation of exponential functions
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
剑指 Offer 28. 对称的二叉树
随机推荐
适用于XP的DDK
Mongodb index
[clean up the extraordinary image of Disk C]
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
556. 下一个更大元素 III
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
The mail function of LNMP environment cannot send mail
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
分布式事务(Seata) 四大模式详解
牛客网:过河卒
puzzle(016.3)千丝万缕
Learn to punch in today
ConstraintLayout 的使用
Preliminary summary of structure
tonybot 人形机器人 首次开机 0630
String reverse order
How to query the baby category of tmall on Taobao
基因家族特征分析 - 染色体定位分析
Luogu p5194 [usaco05dec]scales s solution
ZABBIX saves the page blank after adding calculated items