开发者

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

开发者 https://www.devze.com 2023-02-11 03:56 出处:网络
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:

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:

#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:

#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 err开发者_C百科or on Windows 64 bit Vista, MS VS2008. What bothers me is that

  1. This error is not seen on linux, same application built with RHEL5 gcc4.1.2
  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? 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. This confuses me.


Shouldn't __declspec(dllimport) be __declspec(dllexport) ?


I figured out the answer to this.. yes, it is related to what OJ wrote. Basically I was missing a cmake definition that activated a condition that defined a symbol to __declspec(dllexport). Thanks anyway to @OJ for his pointer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号