Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Microsoft Visual Studio 2008 C++;错误LNK2001,Windows Vista 64位_C++_Visual Studio 2008_Windows Vista_64 Bit - Fatal编程技术网

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

C++ Microsoft Visual Studio 2008 C++;错误LNK2001,Windows Vista 64位,c++,visual-studio-2008,windows-vista,64-bit,C++,Visual Studio 2008,Windows Vista,64 Bit,在尝试将应用程序链接到“第三方库”时,我遇到以下链接器错误,我自己在其中构建了有问题的第三方库。以下是我得到的错误: error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const namesp::classname::V

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

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 
而cpp的相应片段如下所示:

#include <namesp/classname.hpp>
namespace namesp {
    const std::string classname::VARIABLE = "VARIABLE";
}
#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;
    }
}
#包括
名称空间appnamesp{
appclass::somefunc(){
namesp::classname cn;//-编译
namesp::另一个类ac;//-编译
ac.func();//-编译
std::string s=namesp::classname::VARIABLE;//-Linker错误
其他东西;
}
}
这将导致Windows 64位Vista MS VS2008上的链接器错误。有什么困扰 是我吗

  • 在linux上看不到此错误,该应用程序与RHEL5 gcc4.1.2构建的应用程序相同
  • 通过同一导出符号定义的另一个类可用
  • 我做错了什么?这是与静态关键字有关,还是与导出符号有关?我的怀疑是后者,但我有另一个不涉及静态变量的类,该类在我的第三方库中类似地定义,并通过相同的导出符号访问,这不会导致链接器错误,如上所述。
    这让我很困惑。

    不应该是
    \u declspec(dllimport)
    \u declspec(dllexport)

    我找到了答案。。是的,这和OJ写的东西有关。基本上,我缺少一个cmake定义,该定义激活了一个条件,该条件定义了一个符号到_declspec(dllexport)。无论如何,感谢@OJ的指点。

    在类中不应该使用
    \uuudeclspec(dllexport)
    \uuudeclspec(dllimport)
    。为客户端定义一个C接口;否则,只有使用与您完全相同的编译器/编译器版本/编译器设置的人才能使用该类,这一点从一开始就不利于动态链接。_uuudeclspec(dllexport)发生了什么事?@Hans,谢谢。但如果是这样,为什么它适用于其他类?不管怎样,我会试试你的建议——谢谢。@Billy——你的建议已经记下来了。以后也会这样。@Hans,@Billy-我对定义导出符号的帖子做了一个小编辑。请注意,事实证明,有一个额外的条件决定了导出符号的确切定义。在实际解决方案中,整个#ifdef(窗口)#否则部分将变灰。。那么这就是问题所在吗?谢谢谢谢@OJ。我要试试这个。但它并没有向我解释为什么同样的东西适用于namesp::anotherclass ac,它也使用相同的导出符号。@OJ…你说得对。请注意,我的帖子有一处更正。好的,伙计。我很快就会深入研究这个问题。你有没有可以上传给我看的项目?谢谢@OJ。上传会很棘手。这可能需要一些时间。我将尝试制作一个复制错误的简化版本。同时,你可以使用帖子中的信息。再次感谢。
    #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;
        }
    }