当前位置:网站首页>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;
}
边栏推荐
- 7-2 and then what time (15 minutes)
- Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
- 7-17 crawling worms (break exercise)
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- 【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
- Preliminary summary of structure
- 7-18 finding the single root of polynomial by dichotomy
- 天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
- DDK for XP
- 7-6 mixed type data format input
猜你喜欢
Adc128s022 ADC Verilog design and Implementation
修改数据库中的记录为什么报这个错
Tonybot humanoid robot checks the port and corresponds to port 0701
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
Pyqt interface production (login + jump page)
Protobuf and grpc
一文了解微分段应用场景与实现机制
Programming language: the essence of type system
Tonybot humanoid robot infrared remote control play 0630
Tonybot Humanoïde Robot Infrared Remote play 0630
随机推荐
7-24 reduction of the simplest fraction (rolling Division)
Zzuli:1043 max
MongoDB索引
Protobuf and grpc
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
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
【7.3】146. LRU缓存机制
Find books ()
洛谷P5194 [USACO05DEC]Scales S 题解
7-23 currency conversion (using array conversion)
Find the sum of the elements of each row of the matrix
7-22 tortoise and rabbit race (result oriented)
Stop asking yourself if you are suitable for software testing
retrofit
1017 a divided by B (20 points)
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
Find specified characters
Why is this error reported when modifying records in the database
Analysis of gene family characteristics - chromosome location analysis
Time conversion ()