简体   繁体   English

无法跨多个DLL在标头中定义单个对象

[英]can't define a single object in header accross several DLL

I am using VS2010 on a solution with 2 DLLS and I'm trying to define a global variable to be used across both DLLS. 我在具有2个DLLS的解决方案上使用VS2010,并且试图定义要在两个DLLS之间使用的全局变量。

I have the following code: 我有以下代码:

header.h
        namespace A
        {   
            extern DLL_A int myInt;
        }

in a a.cpp file in DLL A: 在DLL A中的a.cpp文件中:

#include "header.h"
using namespace A;

DLL_A int A::myInt = 5; //initialisation

in another b.cpp file in DLL A: 在DLL A的另一个b.cpp文件中:

#include "header.h"
using namespace A;
//use myInt for computations in some method, eg myInt++; etc

DLL_A is defined as the usual: DLL_A通常定义为:

#ifdef SOME_DEFINE
#       define DLL_A __declspec(dllexport)
#   else
#       define DLL_A __declspec(dllimport)
#   endif

However what happens is that while debugging in b.cpp, I see in the watch window that &A::myInt and &myInt are different, which means that an (unknown) "myInt" variable is used for computations, while A::myInt is correctly initialized to 5. 但是发生的是,在b.cpp中调试时,我在监视窗口中看到&A :: myInt和&myInt不同,这意味着(未知)“ myInt”变量用于计算,而A :: myInt是正确初始化为5。

can someone explain to me what is going on and how to fix this? 有人可以向我解释发生了什么事以及如何解决此问题? I don't see how it's possible to link properly, because I have 2 different extern variables that are created and I only initialise one. 我看不到如何正确链接,因为我创建了2个不同的外部变量,并且只初始化了一个。

edit : 编辑:

if I change 如果我改变

DLL_A int A::myInt = 5; //initialisation

for 对于

DLL_A int myInt = 5; //initialisation

it won't link 它不会链接

thanks 谢谢

You say " I see in the watch window that &A::myInt and &myInt are different, which means that an (unknown) "myInt" variable is used for computations". 您说:“我在监视窗口中看到&A :: myInt和&myInt不同,这意味着(未知)“ myInt”变量用于计算”。

That there is exactly your problem. 那正是你的问题。 using namespace A; means that an unqualified name such a myInt will be looked up in A only after the lookup has failed in the current (ie global) namespace. 意味着只有当前(即全局)名称空间中查找失败 ,才会在A中查找诸如myInt类的不合格名称。 But the debugger shows that ::myInt exist. 但是调试器显示::myInt存在。 Therefore, myInt means ::myInt , the first lookup succeeds, and no second lookup is done for ::A::myInt . 因此, myInt意味着::myInt ,第一次查找成功,并且::A::myInt没有第二次查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么办法可以在标题中定义和声明一个外部对象? - Any way I can define and declare an extern object in header? 多重定义-为什么我不能在标题中定义它们 - Multiple Definition - Why can't I define them in a header 跨 DLL 边界的严格混叠 - Strict aliasing accross DLL boundary 在头文件中定义const对象 - Define const object in a header file 尝试Call Dll,但是该方法具有复杂的结构类型,我无法在.Net中定义它 - Try Call Dll, But the method has a complicated struct type, I can't define it in .Net 是否可以包含多个头文件,这些头文件定义具有相同名称的复杂结构? - Is it possible to include several header files that define complex structs with same names? 在单个标头或它们各自的标头文件中定义全局(外部)变量是否更好? - Is it better to define global (extern) variables in a single header, or in their respective header files? DLL:当SDK头文件不使用__declspec(dllexport)时可以使用它吗? - DLL: Can I use __declspec(dllexport) when SDK header file doesn't use it C ++无法定义虚函数OUTSIDE类和头文件 - C++ can't define virtual function OUTSIDE classes and header file 我可以在 header 文件中定义宏吗? - Can I define a macro in a header file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM