一、編譯准備
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.gitgit tag –lgit 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 <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: }
2.VS2010編譯環境配置
在項目屬性—>VC++目錄—>包含目錄 添加boost\和dd\mongo,在庫目錄添加boost\stage\lib和dd\client_build,在連接器—>輸入 添加mongoclient.lib依賴。
編譯運行測試程序,輸出“connected ok”,表明mongoclient編譯沒有問題。
3.編譯問題
(1) mongodb 2.4.3的源碼中包含了他所需要的依賴庫,完全能夠獨立編譯;
(2) mongoclient.lib與msvcprtd.lib重定義問題
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __thiscall std::_Container_base12::~_Container_base12(void)" (??1_Container_base12 <at> std <at> <at> QAE <at> XZ) already defined in mongoclient.lib(log.obj)
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __thiscall std::_Container_base12::_Container_base12(void)" (??0_Container_base12 <at> std <at> <at> QAE <at> 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 <at> _Container_base12 <at> std <at> <at> QAEXXZ) already defined in mongoclient.lib(log.obj)
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "protected: __thiscall std::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 * * __thiscall std::_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,struct std::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 __thiscall std::ios_base::width(__int64)" (?width <at> ios_base <at> std <at> <at> QAE_J_J <at> Z) already defined in mongoclient.lib(log.obj)
1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: __int64 __thiscall std::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 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.cnblogs.com/geosky/archive/2013/05/05/VS2010-MongoDB-Cpp-Driver.html
