【问题标题】:Linker error while compiling mongo db client example on Windows在 Windows 上编译 mongo db 客户端示例时出现链接器错误
【发布时间】:2012-10-30 21:35:02
【问题描述】:

在 Windows 上编译 mongo db 客户端示例时遇到链接器问题。我正在使用 Visual Studio 2012。

我正在尝试从 mongo 的 git 编译 src\mongo\client\examples\clientTest.cpp

我做了以下步骤:

  • 使用 bjam2 构建 Boost v1.51。我在另一个项目中使用它,所以我知道二进制文件很好。
  • 构建 MongoDB C++ 驱动程序为scons --dd mongoclient.lib
  • 在我的项目中包含的 boost 包含目录作为附加包含目录。
  • 定义 _CRT_SECURE_NO_WARNINGS 以避免 MongoDB 客户端代码在使用 strncpy 等时出现警告。
  • 将 boost(二进制)库目录包含到项目中。

我仍然收到以下错误

1>mongoclient.lib(log.obj) : error LNK2019: unresolved external symbol "void __cdecl boost::filesystem3::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 &)" (?convert@path_traits@filesystem3@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::filesystem3::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@filesystem3@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>mongoclient.lib(log.obj) : error LNK2019: unresolved external symbol "private: static class std::codecvt<wchar_t,char,int> const * & __cdecl boost::filesystem3::path::wchar_t_codecvt_facet(void)" (?wchar_t_codecvt_facet@path@filesystem3@boost@@CAAAPBV?$codecvt@_WDH@std@@XZ) referenced in function "public: static class std::codecvt<wchar_t,char,int> const & __cdecl boost::filesystem3::path::codecvt(void)" (?codecvt@path@filesystem3@boost@@SAABV?$codecvt@_WDH@std@@XZ)
1>mongoclient.lib(log.obj) : error LNK2019: unresolved external symbol "class boost::filesystem3::file_status __cdecl boost::filesystem3::detail::status(class boost::filesystem3::path const &,class boost::system::error_code *)" (?status@detail@filesystem3@boost@@YA?AVfile_status@23@ABVpath@23@PAVerror_code@system@3@@Z) referenced in function "bool __cdecl boost::filesystem3::exists(class boost::filesystem3::path const &)" (?exists@filesystem3@boost@@YA_NABVpath@12@@Z)
1>mongoclient.lib(background.obj) : error LNK2019: unresolved external symbol "public: __thiscall boost::thread::~thread(void)" (??1thread@boost@@QAE@XZ) referenced in function "public: class mongo::BackgroundJob & __thiscall mongo::BackgroundJob::go(void)" (?go@BackgroundJob@mongo@@QAEAAV12@XZ)

所以看起来它忽略了 boost 标头中的 #pragma comment lib 指令。

我尝试在项目设置的Linker/Input 选项卡中明确添加libboost_thread-vc110-mt-sgd-1_51.lib。它没有帮助。

我尝试在示例的主 cpp 文件中指定 #pragma comment(lib, "libboost_thread-vc110-mt-sgd-1_51.lib")。它也没有帮助。

但是!添加

#include <boost/thread/thread.hpp>

boost::thread _thrd(&Func);
_thrd.join();

在 clientTest.cpp 的开头帮助消除了关于缺少 boost::thread::~thread(void) 的错误。看起来它强制链接器链接到线程的 lib 二进制文件。

不幸的是,用&lt;boost/filesystem.hpp&gt; 重复这个技巧没有帮助:(

我还 made sure 说,Boost 和 Mongo 客户端的所有 LIB 文件都是使用 dumpbin.exe 为 32 位目标编译的。

还有什么可能是伙计们?

谢谢!

【问题讨论】:

    标签: c++ visual-studio mongodb boost


    【解决方案1】:

    看起来我发给 mongo-dev 邮件列表的消息没有到达目的地。也许它是预先审核的。

    但我设法通过一个有点 hacky 的解决方案使它工作,因为我以前从未使用过 Scons。

    问题的核心是 mongo 预捆绑了 Boost 1.49,而我的系统中安装了 Boost 1.51。我试图针对 boost 1.51 编译示例,而 mongo 是用 1.49 编译的,因此某些类/方法不匹配。

    我通过调用构建库

    scons -j4 --dd --use-system-boost mongoclient.lib

    我还修改了SConstruct 脚本。查找线elif "win32" == os.sys.platform:。在本节中我添加了

    env.Append( EXTRACPPPATH=[ "C:/work/externals/boost_1_51_0" ] )

    指定额外的“系统”文件夹。

    我还必须禁用看起来像的 boost 二进制库的存在性检查

        for b in boostLibs:
            l = "boost_" + b
            if not conf.CheckLib([ l + boostCompiler + "-mt" + boostVersion,
                                   l + boostCompiler + boostVersion ], language='C++' ):
                Exit(1)
    

    因为我的库的命名非常不同。

    当然不包括使用 MSVC 库动态编译 boost 的情况。

    希望 Mongo 团队或熟悉 Scons 的人将构建脚本修改为

    • 允许指定系统 boost 库的路径
    • 允许为 MSVC 构建动态链接库

    【讨论】:

      【解决方案2】:

      Mac OS解决方案

      我知道这里不是发布 Mac OS 答案的地方。但是,我为解决 msgasserted 链接器错误而苦苦挣扎了几天,终于让它工作了......

      mmacosx-version-min=10.5 应该添加到命令行中。

      g++ -m64 -I/Users/accessmac/mgoclient/include -L/Users/accessmac/mgoclient/lib second.cpp -lboost_system-mt -lboost_program_options-mt -lboost_filesystem-mt -lboost_thread-mt -lmongoclient  -mmacosx-version-min=10.5 -o second
      

      编译并生成第二个

      【讨论】:

        猜你喜欢
        • 2011-02-03
        • 2012-02-01
        • 2015-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多