您的位置:首页 > 数据库 > Mongodb

C++ vs2010 mongodb 编译

2014-11-03 16:42 507 查看

1 准备软件:

(1) 下载python2.7,使用x86_32位,因为scons只有32位安装包可用;

(2) 下载scons2.3.0,The current production release of SCons is 2.3.0;大于2.3.0版本的都可以

(3) 安装python 和 scons, 将C:\Python27\Scripts写入PATH;

(4) 下载安装pywin32,It is recommended you install pywin32 if you want todo parallel builds (scons -j)

(5) 下载Mongodb2.4.3的源码:downloads.mongodb.org/src/mongodb-src-r2.4.3.tar.gz (注意:在地址栏中直接输入即可下载 ,如需要其它版本,如2.4.3,在版本处改成相应的版本即可。)

2、安装boost库(1.49版本).

boost1.4.9安装非常简单。

解压后双击bootstrap.bat,生成bjam.exe后,cd到目录c:\boost下,(将boost_1_49更名为boost了)编译boost。

编 译命令:C:\boost>bjam variant=release--with-filesystem--with-thread --with-date_time--with-program_optionsthreading=multi toolset=msvc-10.0 link=static runtime-link=staticaddress-model=32

这是使用VS2010环境编译的release版本,编译完成后,生成C:\boost\stage\lib文件夹,下面有6个lib库:


如 果要编译成debug版本,使用命令:bjam variant=debug --with-filesystem--with-thread --with-date_time--with-program_optionsthreading=multi toolset=msvc-10.0 link=static runtime-link=staticaddress-model=32

3 编译mongoclient.lib

从vs2010的命令提示窗口,进入d:\libs\mongodb-src-r2.4.3源码目录,执行:

scons –-dd --32 mongoclient.lib

在 编译mongoclient.lib的时候,报 “c2065_WIN32_WINNT_VISTA mongo::AtomicIntrinsics<T> 错误”,是因为没有找到新的sdk,加上“--cpppath="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include"” 这个命令就可以正确编译。

如果编译64位 或者release版本,把--dd --> --release,--32-->64即可



注意事项:

1 作用了scons2.3.4 和 mongodb2.6.3编译都失败了。 sconts2.3.0和mongodb2.4.3的版本才可以编译成功。

4、mongoclient应用程序测试

1.1 1.使用vs2010搭建控制台工程

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

   1:  #include <cstdlib>

   2:  #include <iostream>

   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 << "connected ok" << std::endl;

  16:      } 

  17:      catch( const mongo::DBException &e ) 

  18:      {

  19:          std::cout << "caught " << e.what() << std::endl;

  20:      }

  21:      return EXIT_SUCCESS;

  22:  }

1.2 2.VS2010编译环境配置

在项目属性—>VC++目录—>包含目录添加boost\和dd\mongo,在库目录添加boost\stage\lib和dd\client_build,在连接器—>输入 添加mongoclient.lib依赖。

编译运行测试程序,输出“connected ok”,表明mongoclient编译没有问题。


1.3 3.编译问题

(1) mongodb 2.4.3的源码中包含了他所需要的依赖库,完全能够独立编译;

(2) mongoclient.lib与msvcprtd.lib重定义问题

1>msvcprtd.lib(MSVCP100D.dll): error LNK2005: "public: __thiscallstd::_Container_base12::~_Container_base12(void)" (??1_Container_base12<at> std <at> <at> QAE <at> XZ) already defined inmongoclient.lib(log.obj)

1>msvcprtd.lib(MSVCP100D.dll): error LNK2005: "public: __thiscallstd::_Container_base12::_Container_base12(void)" (??0_Container_base12<at> std <at> <at> QAE <at> XZ) already defined inmongoclient.lib(log.obj)

1>msvcprtd.lib(MSVCP100D.dll): error LNK2005: "public: void __thiscallstd::_Container_base12::_Orphan_all(void)" (?_Orphan_all <at>_Container_base12 <at> std <at> <at> QAEXXZ) already definedin mongoclient.lib(log.obj)

1>msvcprtd.lib(MSVCP100D.dll): error LNK2005: "protected: __thiscallstd::basic_streambuf<char,struct std::char_traits<char>>::basic_streambuf<char,struct std::char_traits<char>>(void)" (??0?$basic_streambuf <at> DU?$char_traits <at> D <at>std <at>
<at> <at> std <at> <at> IAE <at>XZ) already defined in mongoclient.lib(log.obj)


1>msvcprtd.lib(MSVCP100D.dll): error LNK2005: "public: struct std::_Iterator_base12 * * __thiscallstd::_Container_base12::_Getpfirst(void)const " (?_Getpfirst <at>_Container_base12 <at> std <at> <at> QBEPAPAU_Iterator_base12<at> 2 <at> XZ) already
defined in mongoclient.lib(log.obj)


1>msvcprtd.lib(MSVCP100D.dll): error LNK2005: "public: void __thiscall std::basic_ios<char,structstd::char_traits<char> >::setstate(int,bool)" (?setstate<at> ?$basic_ios <at> DU?$char_traits <at> D <at> std<at> <at> <at> std <at> <at> QAEXH_N <at>Z)
already defined in mongoclient.lib(log.obj)


1>msvcprtd.lib(MSVCP100D.dll): error LNK2005: "public: __int64 __thiscallstd::ios_base::width(__int64)" (?width <at> ios_base <at> std<at> <at> QAE_J_J <at> Z) already defined inmongoclient.lib(log.obj)

1>msvcprtd.lib(MSVCP100D.dll): error LNK2005: "public: __int64 __thiscallstd::basic_streambuf<char,struct std::char_traits<char>>::sputn(char const *,__int64)" (?sputn <at> ?$basic_streambuf<at> DU?$char_traits <at> D <at> std <at> <at><at> std <at>
<at> QAE_JPBD_J <at> Z) already definedin 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++"\"CodeGeneration"\"Runtime Library" to "Multi-threaded Debug(/MTd)";

2) In your project settings in Visual Studio,change "ConfigurationProperties"\"Linker"\"Input"\"AdditionalDependencies" to "ws2_32.lib;psapi.lib";

3) In your project settings in Visual Studio,change "ConfigurationProperties"\"Linker"\"Input"\"Ignore SpecificDefault 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 musthave 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 externalsymbol __imp_SymGetModuleInfo64

1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved externalsymbol __imp_SymInitialize

1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved externalsymbol __imp_StackWalk64

1>mongoclient.lib(stacktrace.obj) : error LNK2001: unresolved externalsymbol __imp_SymFromAddr


1.4

参考文章:

1 编译时报windows版本错误的解决方法



2
vs2010编译mogodb驱动


以下为转载文章
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: