简体   繁体   English

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

[英]Microsoft Visual Studio 2008 C++ error LNK2001, Windows Vista 64 bit

I am getting the following linker error when trying to link an application to a "3rd-party library", where I myself build the 3rd-party library in question. 尝试将应用程序链接到“第三方库”时,出现以下链接器错误,我自己在其中构建了相关的第三方库。 Here is the error I get: 这是我得到的错误:

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 

The variable in question is defined in a class which is built as part of the 3rd party library. 所讨论的变量在一个类中定义,该类是第三方库的一部分。 Here is the snippet of the class header as concerns the variable in question: 这是有关该变量的类头的代码段:

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

while the corresponding snippet of the cpp is as follows: 而cpp的相应代码段如下:

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

The export symbol is defined in a separate header as follows: 导出符号在单独的标头中定义,如下所示:

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

Finally, I am using it in my application cpp as follows, say: 最后,我在应用程序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;
    }
}

This results in the linker error on Windows 64 bit Vista, MS VS2008. 这会导致Windows 64位Vista,MS VS2008上的链接器错误。 What bothers me is that 令我困扰的是

  1. This error is not seen on linux, same application built with RHEL5 gcc4.1.2 在使用RHEL5 gcc4.1.2构建的相同应用程序的linux上看不到此错误
  2. Another class defined via same export symb is usable.. 通过相同的导出符号定义的另一个类是可用的。

What am I doing wrong? 我究竟做错了什么? Is this something to do with the static keyword, or is it the export symbol? 这与static关键字有关,还是导出符号? My suspicion is the latter, but then I have another class that doesn't involve static variables that is defined similarly in my 3rd party library and accessed via the same export symbol which does not lead to linker errors, as indicated above. 我怀疑是后者,但是如上所述,我还有另一个不涉及静态变量的类,该类在我的第3方库中进行了类似的定义,并且可以通过相同的导出符号访问,而不会导致链接器错误,如上所述。 This confuses me. 这使我感到困惑。

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

I figured out the answer to this.. yes, it is related to what OJ wrote. 我想出了答案。是的,这与OJ撰写的内容有关。 Basically I was missing a cmake definition that activated a condition that defined a symbol to __declspec(dllexport). 基本上,我缺少一个cmake定义,该定义激活了一个条件,该条件将符号定义为__declspec(dllexport)。 Thanks anyway to @OJ for his pointer. 无论如何,感谢@OJ的指针。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM