设为首页 收藏本站
查看: 1400|回复: 0

[经验分享] Visual Studio 2010 编译 MongoDB C++ Client Driver

[复制链接]
YunVN网友  发表于 2015-7-6 09:01:23 |阅读模式
一、编译准备

1. 安装scons
  (1) 下载python2.7, 使用x86_32位,因为scons只有32位安装包可用;
  (2) 下载scons2.3.0,The current production release of SCons is 2.3.0;
  (3)  安装python 和 scons, 将C:\Python27\Scripts写入PATH;
  (4) 下载安装pywin32 ,It is recommended you install pywin32 if you want to do parallel builds (scons -j)

2. 安装boost库
  (1) 下载boost 1.49库
  (2) 编译boost 1.49 , see boost and windows, https://wiki.10gen.com/pages/viewpage.action?pageId=12157032&navigatingVersions=true

  Building Yourself


  • Download the boost source from boost.org. Move it to C:\boost\.
  • From the Visual Studio 2008 IDE, choose Tools.Visual Studio Command Prompt to get a command prompt with all PATH variables set nicely for the C++ compiler.
  • From the MongoDB source project, run buildscripts\buildboost.bat. Or, buildboost64.bat for the 64 bit version.
  When using bjam, MongoDB expects


  • variant=debug for debug builds, and variant=release for release builds
  • threading=multi
  • link=static runtime-link=static for release builds
  • address-model=64 for 64 bit





  • 下面是我的编译命令:
      bjam stage variant=debug  --with-filesystem --with-thread --with-date_time --with-program_options --layout=versioned threading=multi toolset=msvc-10.0 --build-type=complete




  • 编译后boost/stage/lib文件夹下生产5个dll和15个lib文件. see these links for more details:
    http://www.boost.org/doc/libs/1_53_0/more/getting_started/windows.html#or-build-binaries-from-source         
    https://wiki.10gen.com/pages/viewpage.action?pageId=12157032&navigatingVersions=true

3. 下载MongoDB源码
  下载Mongodb 2.4.3源码, see http://www.mongodb.org/downloads


git clone git://github.com/mongodb/mongo.git
git tag –l
git checkout r2.4.3

二、编译Mongoclient.lib

1. 准备C++编译环境
  启动cmd,到vc目录下运行vcvarsall.bat;或者开始—>程序—>\Microsoft Visual Studio 2010\Visual Studio Tools—>Visual Studio 命令提示(2010)
mongodb源码中包含了boost,js等依赖库,可以直接编译。

2.编译mongodb源文件
  编译mongoclient的命令很简单:

  scons –-dd --32 mongoclient.lib // build C++ client driver library

  Add --64 or --32 to get the 64- and 32-bit versions, respectively. Replace --release with --dd to build a debug build.
  编译后在mongodb\build\win32\32\dd\client_build\生成mongoclient.lib.

3.整理mongoclient依赖环境
  将mongo源码中src\mongo复制到mongodb\build\win32\32\dd作为应用程序的头文件目录,形成dd\client_build、dd\mongo.

三、mongoclient应用程序测试

1.使用vs2010搭建控制台工程
  复制src\mongo\client\examples\clientTest.cpp到工程文件夹下,添加这个cpp,形成一个完整的控制台工程。或者使用下面测试代码:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-cpp-driver/









   1:  #include
   2:  #include
   3:  #include "mongo/client/dbclient.h"
   4:   
   5:  void run()
   6:  {
   7:      mongo::DBClientConnection c;
   8:      c.connect("localhost"); //your host IP address
   9:  }
  10:  int main()
  11:  {
  12:      try
  13:      {
  14:          run();
  15:          std::cout msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __thiscall std::_Container_base12::_Container_base12(void)" (??0_Container_base12  std   QAE  XZ) already defined in mongoclient.lib(log.obj)
  1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: void __thiscall std::_Container_base12::_Orphan_all(void)" (?_Orphan_all  _Container_base12  std   QAEXXZ) already defined in mongoclient.lib(log.obj)
  1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "protected: __thiscall std::basic_streambuf::basic_streambuf(void)" (??0?$basic_streambuf  DU?$char_traits  D  std    std   IAE  XZ) already defined in mongoclient.lib(log.obj)
  1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: struct std::_Iterator_base12 * * __thiscall std::_Container_base12::_Getpfirst(void)const " (?_Getpfirst  _Container_base12  std   QBEPAPAU_Iterator_base12  2  XZ) already defined in mongoclient.lib(log.obj)
  1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: void __thiscall std::basic_ios::setstate(int,bool)" (?setstate  ?$basic_ios  DU?$char_traits  D  std    std   QAEXH_N  Z) already defined in mongoclient.lib(log.obj)
  1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __int64 __thiscall std::ios_base::width(__int64)" (?width  ios_base  std   QAE_J_J  Z) already defined in mongoclient.lib(log.obj)
  1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __int64 __thiscall std::basic_streambuf::sputn(char const *,__int64)" (?sputn  ?$basic_streambuf  DU?$char_traits  D  std    std   QAE_JPBD_J  Z) already defined in mongoclient.lib(log.obj)
  ………………



  • 解决办法:http://comments.gmane.org/gmane.comp.db.mongodb.user/68910
      1)  In your project settings in Visual Studio, change "Configuration Properties"\"C/C++"\"Code Generation"\"Runtime Library" to "Multi-threaded Debug (/MTd)";
      2)  In your project settings in Visual Studio, change "Configuration Properties"\"Linker"\"Input"\"Additional Dependencies" to "ws2_32.lib;psapi.lib";
      3)  In your project settings in Visual Studio, change "Configuration Properties"\"Linker"\"Input"\"Ignore Specific Default Libraries" to "msvcrtd";


  (3) 找不到libboost-thread-vc100-mt-sgd-1_49.lib之类问题




  • 这个错误是由于boost库没有对应编译好的缘故,缺少什么库就重新编译相应库,boost的命名规则参考:http://www.boost.org/doc/libs/1_53_0/more/getting_started/windows.html#or-build-binaries-from-source
  (4) mongoclient.lib中无法解析的外部符号,链接错误




  • 这个错误是由于mongoclient.lib找不到相应依赖库,在VS2010属性—>连接器—>输入中添加需要的链接库,注意mongoclient如果是debug版加入的boost依赖库亦应该是debug版。
  (5) AssertionError  target and source lists must have same




  • 这个错误是由于使用scons命令编译mongo时添加了—full标识,是一个小bug,但是其实相应的编译工作已经完成,scons只是没能将编译后的lib文件复制到指定目录。
  (6) mongo源码的编译debug-32或release-32要与boost库的编译对应起来
  (7) stacktrace.obj中无法解析的变量,在工程依赖库中添加Dbghelp.lib

  1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_SymCleanup
1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_SymGetModuleInfo64
1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_SymInitialize
1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_StackWalk64
1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved external symbol __imp_SymFromAddr

  转自:http://www.iyunv.com/geosky/archive/2013/05/05/VS2010-MongoDB-Cpp-Driver.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-83629-1-1.html 上篇帖子: MongoDB结合Flexgrid的简单数据呈现 下篇帖子: 用WPF+MongoDB开发房产信息收集器(2)——后台线程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表