dll链接不一致不允许定义dllimport静态数据成员 [英] inconsistent dll linkage & definition of dllimport static data member not allowed

查看:598
本文介绍了dll链接不一致不允许定义dllimport静态数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这两个文件:



Header.h

  class DLL ExportClass {
public:
ExportClass();
静态int测试;
};

Source.cpp

  #ifdef导出
#定义DLL __declspec(dllexport)
#else
#定义DLL __declspec(dllimport)
#endif

#include Header.h

int ExportClass :: test = 0;
ExportClass :: ExportClass(){
}

我不会定义 EXPORT (要导入具有 static 成员的已导出类),为什么会收到以下警告:

  1> source.cpp(11):警告C4273:'test':DLL链接不一致
1> header.h(4):参见 public:static int ExportClass :: test
1> source.cpp(13):警告C4273:'ExportClass :: ExportClass':dll链接不一致
1> header.h(3):请参见 {ctor}的先前定义

此错误:

  1> source.cpp(11):错误C2491:'ExportClass :: test':dllimport静态数据成员的定义不允许

如果我定义了 EXPORT ,它就可以工作。我有点理解警告,但我认为,编译器可以忽略静态变量和ctor,因为无论如何整个类都声明为 __ declspec(dllimport) 。我想对 __ declspec(dllexport) __ declspec(dllimport)使用相同的代码库-但似乎编译器stll尝试定义在其声明中标记为 __ declspec(dllexport)的这些符号。解决此问题的常用方法是什么?

解决方案

您期望编译器忽略一个非常严重的事故。它在类声明中遇到了__declspec(dllimport)属性,该属性明确表示该类实现存在于要在运行时绑定的不同模块中。但是随后它也遇到了定义,这完全是意外的,因为属性合同说它是在完全不同的项目中编译的。



会生成C4273警告,以提醒您尚不清楚在运行时实际上要执行什么功能。 DLL中有两个,一个正在忙着编译,另一个在忙着编译。实际执行的是哪一个? C4273是1级警告,适合这肯定是错误的类别。可以正常工作并不是完全不可能的,因为人们期望 函数至少具有相同的代码。但是,不会造成麻烦的可能性并不大,只有在该函数没有更改内部DLL状态的任何副作用的情况下,它才可能起作用。

然后遇到导出的变量,这很难诊断。同样的情况,有两个。这是编译器程序员放下脚步的地方,让代码随机使用一个或另一个不再是可以忽略的事情。那将永远无法工作,变量不能具有相同的值。因此,C2491是一个硬错误。



不知道您是如何进入这个泡菜的,很显然,您要经过的道路会让您从陡峭的悬崖上掉下来。 / p>

Assuming I have these two files:

Header.h

class DLL ExportClass{
public:
  ExportClass();
  static int test;
};

Source.cpp

#ifdef EXPORT
    #define DLL __declspec(dllexport)
#else
    #define DLL __declspec(dllimport)
#endif

#include "Header.h"

int ExportClass::test = 0;
ExportClass::ExportClass(){
}

And I won't define EXPORT (to import a already exported class with a static member), why do I get these warnings:

1>source.cpp(11): warning C4273: 'test' : inconsistent dll linkage
1>          header.h(4) : see previous definition of 'public: static int ExportClass::test'
1>source.cpp(13): warning C4273: 'ExportClass::ExportClass' : inconsistent dll linkage
1>          header.h(3) : see previous definition of '{ctor}'

And this error:

1>source.cpp(11): error C2491: 'ExportClass::test' : definition of dllimport static data member not allowed

If I define EXPORT it works. I kind of understand the warnings, but I thought, that the static variable and the ctor could be ignored by the compiler, because the whole class is declared as __declspec(dllimport) anyway. I want to use the same codebase for the __declspec(dllexport) and __declspec(dllimport) - but it seems the compiler stll tries to define these symbols that are marked as __declspec(dllexport) in their declaration. What is the common practice to solve this problem?

解决方案

You are expecting the compiler to ignore a very serious mishap. It encountered the __declspec(dllimport) attribute on the class declaration, that quite unequivocally states that the class implementation is present in different module that's going to bound at runtime. But then it encountered the definition as well, completely unexpected since the attribute contract says that it is compiled in an entirely different project.

The C4273 warning is generated to remind you that it is very unclear what function is actually going to execute at runtime. There are two, one that is busy compiling, another in the DLL. Which one will actually execute is a wild guess. C4273 is a level 1 warning, the kind that fit the "this is almost surely wrong" category. It is not entirely impossible to work okay since there's some expectation that the functions have at least the same code. The odds that will not cause trouble are however not great, it could only work if the function doesn't have any side effects that change the internal DLL state. Very hard to diagnose bug when it does btw.

Then it encountered the exported variable. Same case, there are two of them. This is where the compiler programmer put his foot down, having code randomly use one or the other is no longer something that can be ignored. That just cannot ever work, the variables cannot have the same value. So C2491 is a hard error.

No idea how you got in this pickle, clearly the road you're trying to travel will make you fall off a steep cliff.

这篇关于dll链接不一致不允许定义dllimport静态数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆