在Windows上编译mongo db客户端示例时链接器错误

时间:2012-10-30 21:35:03

标签: c++ visual-studio mongodb boost

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

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

我做了以下步骤:

  • 使用bjam2构建Boost v1.51。我在另一个项目上使用它,所以我知道二进制文件很好。
  • 将MongoDB C ++驱动程序构建为scons --dd mongoclient.lib
  • 包含的提升包括我的项目中的目录作为附加包含目录。
  • 定义_CRT_SECURE_NO_WARNINGS以避免在使用strocpy时发出警告,例如MongoDB客户端代码。
  • 将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指令。

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

我尝试在示例的主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二进制文件。

不幸的是,用<boost/filesystem.hpp>重复这个技巧并没有帮助:(

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

还有什么人呢?

谢谢!

2 个答案:

答案 0 :(得分: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" ] )

指定其他“system”文件夹。

另外,我必须禁用对二进制库的存在检查,看起来像

    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的人会将构建脚本修改为

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

答案 1 :(得分:0)

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

编译并生成第二个