【问题标题】:Linker error LNK2019 when linking Boost 1.51.0 dynamically in VS2010在 VS2010 中动态链接 Boost 1.51.0 时出现链接器错误 LNK2019
【发布时间】:2012-09-24 17:52:54
【问题描述】:

我正在使用由 BoostPro Computing 安装程序安装的 boost 库。我在 Windows 7 64 位机器上使用 VS 2010。我想动态链接到boost,所以我在安装程序中选择了前两个选项(多线程调试DLL和多线程DLL,我相信它们被调用了)。一些已安装库的示例如下:

boost_bzip2-vc100-mt-1_51.lib
boost_bzip2-vc100-mt-gd-1_51.lib

在我的项目中链接到 boost 时,我还确保定义了BOOST_ALL_DYN_LINK。我专门使用filesystem 工具集。

当我打开 BOOST_LIB_DIAGNOSTIC 时,我在构建输出中看到以下消息:

1>  Linking to lib file: boost_filesystem-vc100-mt-gd-1_51.lib
1>  Linking to lib file: boost_system-vc100-mt-gd-1_51.lib

然而,这些很快就会被跟进:

1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const __thiscall boost::filesystem::path::string(void)const " (__imp_?string@path@filesystem@boost@@QBE?BV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall boost::filesystem::path::~path(void)" (__imp_??1path@filesystem@boost@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) class boost::filesystem::path __cdecl boost::filesystem::detail::unique_path(class boost::filesystem::path const &,class boost::system::error_code *)" (__imp_?unique_path@detail@filesystem@boost@@YA?AVpath@23@ABV423@PAVerror_code@system@3@@Z) referenced in function "class boost::filesystem::path __cdecl boost::filesystem::unique_path(class boost::filesystem::path const &)" (?unique_path@filesystem@boost@@YA?AVpath@12@ABV312@@Z)
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class std::codecvt<wchar_t,char,int> const & __cdecl boost::filesystem::path::codecvt(void)" (__imp_?codecvt@path@filesystem@boost@@SAABV?$codecvt@_WDH@std@@XZ) referenced in function "public: __thiscall boost::filesystem::path::path<char const [20]>(char const (&)[20],void *)" (??$?0$$BY0BE@$$CBD@path@filesystem@boost@@QAE@AAY0BE@$$CBDPAX@Z)
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl boost::filesystem::path_traits::convert(char const *,char const *,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > &,class std::codecvt<wchar_t,char,int> const &)" (__imp_?convert@path_traits@filesystem@boost@@YAXPBD0AAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@ABV?$codecvt@_WDH@5@@Z) referenced in function "void __cdecl boost::filesystem::path_traits::dispatch<class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > &,class std::codecvt<wchar_t,char,int> const &)" (??$dispatch@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@path_traits@filesystem@boost@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@4@ABV?$codecvt@_WDH@4@@Z)
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (__imp_?generic_category@system@boost@@YAABVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'posix_category''(void)" (??__Eposix_category@system@boost@@YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) class boost::system::error_category const & __cdecl boost::system::system_category(void)" (__imp_?system_category@system@boost@@YAABVerror_category@12@XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'native_ecat''(void)" (??__Enative_ecat@system@boost@@YAXXZ)

auto-link.hpp 不应该为我处理我的链接吗?我没有特别要求将任何内容链接到该项目,因为自动链接器似乎正确识别了所有内容。那么我怎么会错过这些东西呢?另外,它们被声明为dllimport,所以链接器不应该不理会它们并期望它们在运行时被发现吗?

谢谢!

更新:我决定深入研究第二个链接器错误。基本上是说它找不到path 类的析构函数。在filesystem 库上运行dumpbin 后,我注意到文件中的这一行

??1path@filesystem@boost@@QEAA@XZ (public: __cdecl boost::filesystem::path::~path(void))

但这显然与链接器正在查找的内容不匹配,即:

"__declspec(dllimport) public: __thiscall boost::filesystem::path::~path(void)" (__imp_??1path@filesystem@boost@@QAE@XZ)

请注意,链接器正在寻找 DLL 导入版本,但库本身似乎没有提供...不知道从哪里开始,但这似乎是重要的信息!

【问题讨论】:

    标签: c++ visual-studio-2010 visual-studio boost linker


    【解决方案1】:

    对我来说,这是因为“将 wchar_t 视为内置类型”选项设置为 false,因此 wchar_t 被编译为 unsigned short

    【讨论】:

    【解决方案2】:

    假设您已经编译了那些 .lib,您需要确保 .lib 文件位于库路径中(查看 VC++ 目录->库路径)。

    编译器将在编译时放置指向 DLL 的链接,使用 .lib 来发现正确的入口点等,因此当 EXE/DLL 在运行时启动时可以有效地加载它们。

    您所说的运行时 DLL 发现类型需要 LoadLibrary + GetProcAddress 类型代码,Boost 不支持。

    (静态链接实际上是将静态编译的 .lib 代码中的代码放入您的 DLL/EXE 中。)

    编辑:另外,请检查您是否为您的架构使用了正确的 .lib 文件,例如32 位或 64 位。这会导致签名出现类似错误。

    dumpbin /headers 
    

    会告诉你 .lib 是为哪个“机器”类型构建的(dumpbin 输出的第一部分)。

    【讨论】:

    • 感谢您的建议。这些库确实在那里——我测试了删除“链接到库...”中提到的一个库,我收到一条错误消息,指出文件丢失。所以提到的文件确实被正确链接到,但似乎缺少一些符号或定义?
    • .libs 是否使用 Unicode 编译,因为它看起来像是在链接多字节字符集?如果您在 .lib 上执行 dumpbin /ALL,您可以看到正在导出的内容。如果所有的类都使用 wchar_t 你就知道问题所在了。
    • 好建议——我会看看!
    • Hrm...将项目切换为使用 Unicode 无效...同样的链接器错误!
    • 另一个想法...您确定您使用的是 32 位 .lib 文件。这会导致签名出现类似错误。
    【解决方案3】:

    (从您的未解决列表中)调用约定有所不同。这将导致符号不匹配。我发现如果您尝试使用 _cdecl 作为 Windows 上的调用约定以外的方式进行编译,1.54.0 将不会编译所有库。 Windows 喜欢许多不同的调用约定,它们不会匹配(_cdecl /Gd、__stdcall /Gz、__FASTCALL /Gr) 此外,至少在 1.54.0 中,我注意到某些库需要将 wchar_t 视为内置类型(Windows VS 选项 /Zc:wchar_t)(肯定是 boost::log 库)。这也会导致未解决的错误,因为在这种情况下 wchar_t 与 unsigned short 不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多