从dll链接静态成员



我在使用Visual Studio Express 2010链接dll中的静态成员时遇到了一个奇怪的问题。Linux平台而MSYS/MinGW (GCC)则不会出现此错误。

我有一个数学库,使用导出宏显式地为这个库模块:

#ifdef WIN32
  #ifdef MATH_LIBRARY_EXPORT
    #define MATH_LIBRARY_API __declspec(dllexport)
  #else
   #define MATH_LIBRARY_API __declspec(dllimport)
  #endif
#else
  //define empty values for linux OS
  #define MATH_LIBRARY_API
#endif

这是我的Vector类i导出的静态成员的剪辑:

ifndef BINREV_VECTOR_H__
#define BINREV_VECTOR_H__
// include common header with dll import/export macro
#include <brMath/brCommons.h>
namespace binrev{
namespace brMath{
class MATH_LIBRARY_API brVector3f
{  
  public:
  float m_fX, m_fY, m_fZ;
  brVector3f(void);
  brVector3f(float x, float y, float z);
  ...
  public:
  static const brVector3f ZERO;
  static const brVector3f NEGATIVE_UNIT_Z;
  ...
};

和cpp模块:

// Ensure that the dll hader will be exported
#define MATH_LIBRARY_EXPORT
#include <brMath/brVector3f.h>
namespace binrev{
namespace brMath{
const brVector3f brVector3f::ZERO(0.0f, 0.0f, 0.0f);
const brVector3f brVector3f::NEGATIVE_UNIT_Z( 0.0f, 0.0f, -1.0f);
...

在我的图形模块(也是一个dll与不同的显式导出宏)使用这个数学dll我尝试访问这些静态成员之一:

#include <brMath/brVector3f.h>
brMath::brVector3f brCamera::getDirection(void)
{
   return  m_orientation.rotate(brMath::brVector3f::NEGATIVE_UNIT_Z);
}

在其他平台上任何工作都很好,但与MVSE 2010我得到链接器失败:

1>------ Erstellen gestartet: Projekt: ZERO_CHECK, Konfiguration: Debug Win32 ------
2>------ Erstellen gestartet: Projekt: brGraphics, Konfiguration: Debug Win32 ------
2>  brCamera.cpp
2>brCamera.obj : error LNK2001: Nicht aufgelöstes externes Symbol ""public: static   class binrev::brMath::brVector3f const binrev::brMath::brVector3f::NEGATIVE_UNIT_Z"  (?NEGATIVE_UNIT_Z@brVector3f@brMath@binrev@@2V123@B)".
2>C:binrevrepositorybinrevenginemodulesbrGraphicstrunkbinbrGraphics.dll : fatal error LNK1120: 1 nicht aufgelöste externe Verweise.
========== Erstellen: 1 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========

我不是MVSE的朋友,这是我第一次尝试让我们的代码与MVSE可运行。虽然我有不同的解决方案,我已经添加了brMath。Lib作为项目设置中的附加依赖项。我还将附加库目录的路径设置为brMath.lib的位置。以下是用于查看设置的链接器命令的输出:

/OUT:"C:binrevrepositorybinrevenginemodulesbrGraphicstrunkbinbrGraphics.dll"    /INCREMENTAL /NOLOGO
/LIBPATH:"C:binrevrepositorybinrevenginemodulesbrMathtrunklibDebug" /DLL   "kernel32.lib" "user32.lib" "gdi32.lib"
"winspool.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "comdlg32.lib"    "advapi32.lib" "brCore.lib" "brMath.lib"
"c:binrevdevelopmentvs2010VCliblibboost_signals-vc100-mt-gd-1_47.lib" "c:binrev developmentvs2010VCliblibboost_system-vc100-mt-gd-1_47.lib"
/MANIFEST /ManifestFile:"brGraphics.dirDebugbrGraphics.dll.intermediate.manifest"   /ALLOWISOLATION
/MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:binrevrepository  binrevenginemodulesbrGraphicstrunkbinbrGraphics.pdb"
/SUBSYSTEM:CONSOLE /STACK:"10000000" /PGD:"C:binrevrepositorybinrevenginemodulesbrGraphicstrunkbinbrGraphics.pgd" /TLBID:1
/DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/binrev/repository/binrevengine/modules/brGraphics  /trunk/lib/Debug/brGraphics.lib" /MACHINE:X86 /ERRORREPORT:QUEUE

似乎一切都是正确的。当我删除brVector3f的const static dll成员的调用时,构建成功了。我认为const static成员一定有问题。但是为什么只使用MVSE呢?

我找不出这个介绍有什么不同:http://stackoverflow...c-data-in-a-dll所以它应该正常工作……

现在我不知道哪里出了问题。对于任何帮助或提示,我都很感激。

"…"当在文件范围内修改变量或函数时,static关键字指定该变量或函数具有内部链接(其名称在声明它的文件外部是不可见的)。

最新更新