当前位置:网站首页>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)
- ConstraintLayout 的使用
- 7-20 print 99 formula table (format output)
- Although not necessarily the best, it must be the hardest!
- Paper sharing: generating playful palettes from images
- Use of constraintlayout
- 洛谷P3065 [USACO12DEC]First! G 题解
- 适用于XP的DDK
- The mail function of LNMP environment cannot send mail
- Table of mathematical constants by q779
猜你喜欢

必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿

分布式事务(Seata) 四大模式详解

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
![Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution](/img/89/da1a3a38e02671628f385de0f30369.png)
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution

puzzle(016.3)千丝万缕

Paper sharing: generating playful palettes from images

Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions

一文了解微分段应用场景与实现机制
随机推荐
Common commands for getting started with mongodb database
7-3 count the number of words in a line of text
Zzuli:1045 numerical statistics
Puzzle (016.3) is inextricably linked
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
7-23 currency conversion (using array conversion)
洛谷P4047 [JSOI2010]部落划分 题解
修改数据库中的记录为什么报这个错
Sendmail无法发送邮件及发送过慢解决
Raft agreement
Thread. Sleep and timeunit SECONDS. The difference between sleep
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
556. The next larger element III
Although not necessarily the best, it must be the hardest!
Time conversion ()
光猫超级账号密码、宽带账号密码 获取
动态获取权限
Find specified characters
Zabbix添加Calculated items后保存页面成空白
7-22 tortoise and rabbit race (result oriented)