当前位置:网站首页>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;
}
边栏推荐
- LNMP环境mail函数不能发送邮件解决
- C language,%d% Difference between 2D%2d%02d
- 7-20 print 99 formula table (format output)
- Exercise 10-8 recursive implementation of sequential output of integers
- Tonybot humanoid robot infrared remote control play 0630
- 基因家族特征分析 - 染色体定位分析
- Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
- 添加Zabbix计算类型项目Calculated items
- Use of constraintlayout
- Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
猜你喜欢

Mysql多表查询 #子查询

Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not

X86 assembly language - Notes from real mode to protected mode

Exercise 10-1 calculate the sum of 1 to n using recursive functions

retrofit

7-10 calculate salary

puzzle(016.3)千丝万缕

GRPC的四种数据流以及案例

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

Leetcode(4)——尋找兩個正序數組的中比特數
随机推荐
Convert string to decimal integer
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
剑指 Offer 28. 对称的二叉树
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-3 recursive implementation of exponential functions
C library function - qsort()
puzzle(016.4)多米诺效应
7-20 print 99 formula table (format output)
【7.3】146. LRU缓存机制
Creation of data table of Doris' learning notes
Four data flows and cases of grpc
提高效率 Or 增加成本,开发人员应如何理解结对编程?
常见问题之PHP——ldap_add(): Add: Undefined attribute type in
论文分享:Generating Playful Palettes from Images
Showmebug entered Tencent conference, opening the era of professional technical interview
NPM install is stuck with various strange errors of node NPY
tonybot 人形机器人 定距移动 代码编写玩法
Detailed explanation of four modes of distributed transaction (Seata)
C language,%d% Difference between 2D%2d%02d