Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从DLL导出全局常量变量_C++_Qt_Dll - Fatal编程技术网

C++ 从DLL导出全局常量变量

C++ 从DLL导出全局常量变量,c++,qt,dll,C++,Qt,Dll,以下是我在Qt框架中使用的代码的简化版本: foo.h: // Defines DLL import or export as required #if defined(REGEXUTIL_LIBRARY) #define LIBRARY_EXPORT Q_DECL_EXPORT #else #define LIBRARY_EXPORT Q_DECL_IMPORT #endif LIBRARY_EXPORT extern const QString testString; foo.c

以下是我在Qt框架中使用的代码的简化版本:

foo.h:

// Defines DLL import or export as required
#if defined(REGEXUTIL_LIBRARY)
  #define LIBRARY_EXPORT Q_DECL_EXPORT
#else
  #define LIBRARY_EXPORT Q_DECL_IMPORT
#endif

LIBRARY_EXPORT extern const QString testString;
foo.cpp:

#include "foo.h"
LIBRARY_EXPORT const QString testString = "Test string";
test.cpp,在导入此DLL的模块中:

const QString s = testString;
我正确地知道DLL和测试编译和链接,因为上面的代码使用的是非常量QString。然而,当我生成QString常量时,我在编译测试时得到一个未解决的外部符号错误,我假设这与const变量未在头文件中初始化有关。如果我在那里初始化它,那么当我试图编译测试时,我会得到关于DLL导入前缀不被允许的错误


修复此错误的正确方法是什么?我希望能够从导入此DLL的模块中访问常量QString变量。

如果您可以接受在标头中定义的常量字符串的解决方案,则根本不需要导入符号:

//foo.h
static const QString testString = "Test string";

应该足够了-testString可以在导入dll的模块中访问,也可以在dll本身内部访问。

您是否尝试过不使用“extern”关键字?如果我去掉extern,则使用testString编译时的dll错误已经在foo.obj中定义。这是否意味着如果此标头包含在批中,将出现多个定义错误对于不同的文件?@x6herbius没有,因为它被定义为静态的。哎呀,我没注意到。如果理论上可行,我现在就测试它。这将在整个程序中使用该常数,而不管该常数在DLL中有什么值,它可能会在程序不知道的情况下更新。也就是说,最终你会在DLL和程序中得到不同的值。