当前位置:网站首页>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;
}
边栏推荐
- X86 assembly language - Notes from real mode to protected mode
- 如何查询淘宝天猫的宝贝类目
- 556. 下一个更大元素 III
- 7-20 print 99 formula table (format output)
- 一文了解微分段应用场景与实现机制
- Creation of data table of Doris' learning notes
- Exercise 10-1 calculate the sum of 1 to n using recursive functions
- Tonybot Humanoïde Robot Infrared Remote play 0630
- 7-8 overspeed judgment
- Exercise 10-6 recursively find Fabonacci sequence
猜你喜欢

ShowMeBug入驻腾讯会议,开启专业级技术面试时代

一文了解微分段应用场景与实现机制

Understand the application scenario and implementation mechanism of differential segment

x86汇编语言-从实模式到保护模式 笔记
![Luogu p4047 [jsoi2010] tribal division solution](/img/7f/3fab3e94abef3da1f5652db35361df.png)
Luogu p4047 [jsoi2010] tribal division solution

tonybot 人形機器人 紅外遙控玩法 0630

ConstraintLayout 的使用

tonybot 人形机器人 首次开机 0630

Showmebug entered Tencent conference, opening the era of professional technical interview

Exercise 10-6 recursively find Fabonacci sequence
随机推荐
Find specified characters
修改数据库中的记录为什么报这个错
DDK for XP
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
7-6 mixed type data format input
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
Creation of data table of Doris' learning notes
npm install卡住与node-npy的各种奇怪报错
SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
别再问自己适不适合做软件测试了
Showmebug entered Tencent conference, opening the era of professional technical interview
如何查询淘宝天猫的宝贝类目
String substitution
牛客网:过河卒
String sort
Accelerating strategy learning using parallel differentiable simulation
Mysql多表查询 #子查询
Puzzle (016.4) domino effect
Selective sorting