c++ - Microsoft Visual Studio 2008 C++ 错误 LNK2001,Windows Vista 64 位

标签 c++ visual-studio-2008 windows-vista 64-bit

尝试将应用程序链接到“第 3 方库”时,我收到以下链接器错误,我自己在其中构建了有问题的第 3 方库。这是我得到的错误:

error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > 
const namesp::classname::VARIABLE" (?VARIABLE@classname@namesp@@2V?$basic_string@DU?
$char_traits@D@std@@V?$allocator@D@2@@std@@B) <path\to\mylib>.dll : fatal error LNK1120: 1 unresolved externals 

有问题的变量是在作为第 3 方库的一部分构建的类中定义的。以下是与相关变量相关的类标题片段:

namespace namesp {
    class MY_EXPORT classname {
        public: 
             static const std::string VARIABLE; 
    };
 }

而对应的cpp片段如下:

#include <namesp/classname.hpp>
namespace namesp {
    const std::string classname::VARIABLE = "VARIABLE";
}

导出符号在单独的 header 中定义如下:

#if defined(WINDOWS) && defined(SHARED)
    #if(COND)
        #define MY_EXPORT __declspec(dllexport) 
    #else
        #define MY_EXPORT __declspec(dllimport) 
    #endif
#else
    #define MY_EXPORT
#endif

最后,我在我的应用程序 cpp 中使用它,如下所示:

#include <namesp/classname.hpp>

namespace appnamesp {
    appclass::somefunc() {
       namesp::classname cn; //-Compiles
       namesp::anotherclass ac; //-Compiles
       ac.func();  //-Compiles
       std::string s = namesp::classname::VARIABLE; //-Linker error
       other stuff;
    }
}

这会导致 Windows 64 位 Vista、MS VS2008 上的链接器错误。有什么困扰 我就是那个

  1. 此错误在 linux 上未见,使用 RHEL5 gcc4.1.2 构建的相同应用程序
  2. 可以使用通过相同导出符号定义的另一个类..

我做错了什么?这与 static 关键字有关,还是与导出符号有关?我怀疑是后者,但是我有另一个不涉及静态变量的类,它在我的第 3 方库中以类似方式定义,并通过相同的导出符号访问,这不会导致链接器错误,如上所述。 这让我很困惑。

最佳答案

__declspec(dllimport) 不应该是 __declspec(dllexport) 吗?

关于c++ - Microsoft Visual Studio 2008 C++ 错误 LNK2001,Windows Vista 64 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5024852/

相关文章:

c++ - Visual Studio : Unresolved Token Error while linking CreateILockBytesOnHGlobal()

c++ - 关于将 CG 着色器集成到 GL 和 D3D9 中的教程?

c++ - 以编程方式确定 native .exe 是 32 位还是 64 位

c# - 代码冲刺 : Keyboard Shortcut to Add Using Statement

.net - 在Windows Vista上安装'WCF HTTP Activation'失败?

C# Windows7/Vista 风格的 ListView

c# - C#项目中引用MMDevice API

c++ - 对数空间线性?

c++ - 在 VS2008 中使用 Qt 时,IntelliSense 不能正常工作

c# - 找不到行时使用 CopyToDataTable() 避免异常