当前位置:网站首页>dllexport和dllimport
dllexport和dllimport
2022-07-03 14:34: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;
}
边栏推荐
- 洛谷P3065 [USACO12DEC]First! G 题解
- pyQt界面制作(登录+跳转页面)
- 7-15 calculation of PI
- MongoDB索引
- Time conversion ()
- 7-20 print 99 formula table (format output)
- 7-1 positive integer a+b (15 points)
- Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
- Tonybot humanoid robot checks the port and corresponds to port 0701
- Use of constraintlayout
猜你喜欢
Mysql多表查询 #子查询
Creation of data table of Doris' learning notes
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
一文了解微分段应用场景与实现机制
Detailed explanation of four modes of distributed transaction (Seata)
Niuke: crossing the river
Why is this error reported when modifying records in the database
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
Puzzle (016.3) is inextricably linked
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
随机推荐
556. The next larger element III
光猫超级账号密码、宽带账号密码 获取
Statistical capital consonants
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
Output student grades
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
Thread.sleep和TimeUnit.SECONDS.sleep的区别
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
7-28 monkeys choose King (Joseph problem)
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
Thread. Sleep and timeunit SECONDS. The difference between sleep
tonybot 人形机器人 红外遥控玩法 0630
7-23 currency conversion (using array conversion)
Sword finger offer 28 Symmetric binary tree
Doris学习笔记之数据表的创建
Niuke: crossing the river
Find books ()
JVM garbage collector
DDK for XP
Stop asking yourself if you are suitable for software testing