> 文章列表 > Qt5.12实战之dll中导出变量

Qt5.12实战之dll中导出变量

Qt5.12实战之dll中导出变量

1.通过def方式导出:

在cpp中定义变量

 使用.def文件定义导出变量名

生成lib与dll文件

 

 调用导出库的变量:

指定包含目录与库目录

 指定输入库:

声明变量为外部库导出变量:

 

使用导出变量:

#include <iostream>//声明gExport_Value为dll中导出的变量
extern int _declspec(dllimport) gExport_Value;int main()
{std::cout << "Hello World!\\n";std::cout << "默认值 :"<<gExport_Value;gExport_Value = 666;std::cout << "修改值 :" << gExport_Value;getchar();
}

 

 

2.通过_declspec方式导出:

声明导出变量

#pragma once
//默认导出
#ifdef EXPORT_DLL_VAL
#define EXP _declspec(dllexport) //导出
#else
#define EXP _declspec(dllimport) //导入
#endif // EXPORT_DLL_VAL//保持导出名一致
extern "C"
{extern int EXP  gExportV1;extern int EXP  gExportV2;
}

初始变量并定义导出

//定义宏,以使用导入变量
#ifndef EXPORT_DLL_VAL
#define EXPORT_DLL_VAL
#endif // !EXPORT_DLL_VAL
#include "TestExportVal_Declspec.h"
int gExportV1 = 123;
int gExportV2 = 456;

生成lib与dll

使用:

指定包含目录与库目录

 

 添加依赖库

 

使用导出变量

 

 

 

国学经典