[英]can't define a single object in header accross several DLL
我在具有2個DLLS的解決方案上使用VS2010,並且試圖定義要在兩個DLLS之間使用的全局變量。
我有以下代碼:
header.h
namespace A
{
extern DLL_A int myInt;
}
在DLL A中的a.cpp文件中:
#include "header.h"
using namespace A;
DLL_A int A::myInt = 5; //initialisation
在DLL A的另一個b.cpp文件中:
#include "header.h"
using namespace A;
//use myInt for computations in some method, eg myInt++; etc
DLL_A通常定義為:
#ifdef SOME_DEFINE
# define DLL_A __declspec(dllexport)
# else
# define DLL_A __declspec(dllimport)
# endif
但是發生的是,在b.cpp中調試時,我在監視窗口中看到&A :: myInt和&myInt不同,這意味着(未知)“ myInt”變量用於計算,而A :: myInt是正確初始化為5。
有人可以向我解釋發生了什么事以及如何解決此問題? 我看不到如何正確鏈接,因為我創建了2個不同的外部變量,並且只初始化了一個。
編輯:
如果我改變
DLL_A int A::myInt = 5; //initialisation
對於
DLL_A int myInt = 5; //initialisation
它不會鏈接
謝謝
您說:“我在監視窗口中看到&A :: myInt和&myInt不同,這意味着(未知)“ myInt”變量用於計算”。
那正是你的問題。 using namespace A;
意味着只有在當前(即全局)名稱空間中查找失敗后 ,才會在A
中查找諸如myInt
類的不合格名稱。 但是調試器顯示::myInt
存在。 因此, myInt
意味着::myInt
,第一次查找成功,並且::A::myInt
沒有第二次查找。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.