首页 > 技术文章 > MongoDB的c++驱动安装痛苦历程

hanmolabi 2017-12-25 21:38 原文

环境:win7 64bit     vs2010

看过网上暴多文章的讲解,总结了两条安装mongodb c++驱动的方法。老版本是通过scons,新版本是通过cmake。新版本据说很麻烦,未做尝试,仍使用老版本的scons的编译方法。

过程中遇到了很多问题,其实主要是各种程序的版本问题,现梳理如下,亲测成功:

1.Python2.7.2

http://www.python.org/download/releases/2.7.2/   选择Windows x86 MSI Installer (2.7.2) (sig)

注意安装的时候选择只使用当前用户,否则会在下一个安装程序中报注册表的错。

2. pywin32

http://sourceforge.net/projects/pywin32/files/pywin32/Build216/pywin32-216.win32-py2.7.exe/download

pywin32-216.win32-py2.7.exe

3.  scons-2.1.0.win32.exe

http://sourceforge.net/projects/scons/files/scons/2.1.0/scons-2.1.0.win32.exe/download

4.boost_1.57

https://ncu.dl.sourceforge.net/project/boost/boost-binaries/1.57.0/boost_1_57_0-msvc-10.0-32.exe

 不需要编译就可以直接安装到boost的动态库。

5.08mongo-cxx-driver-26compat

https://codeload.github.com/mongodb/mongo-cxx-driver/zip/26compat

cd到该目录下,运行cmd:

scons --use-system-boost --32 --sharedclient --dynamic-windows --prefix=D:\mongo-client-install --cpppath=D:\01Lib\07boost_1_57_0_32 --libpath=D:\01Lib\07boost_1_57_0_32\lib32-msvc-10.0 --full install-mongoclient

其中cpppath代表boost的安装路径,libpath代表boost lib库的路径,dbg表示编译为debug而不是release版本。

D:\01Lib\mongo-client-install\lib就是库安装的路径。

D:\01Lib\mongo-client-install\include就是包含目录。

 最后给一段测试代码。需设置boost和mongodb的包含文件和库文件目录。

#include "stdafx.h"
#include <iostream>
#include "mongo/client/dbclient.h"
using namespace std;
using namespace mongo;

void run() {
DBClientConnection c;
c.connect("127.0.0.1:27017"); //add port,c.connect("localhost:27017")
const char* ns = "test.first";
c.insert(ns, BSON("name" << "joe"
<< "pwd" << "123456"
<< "age" << 20));
}

int main(void)
{
try {
run();
cout<<"connected ok"<<endl;
system("pause");
}catch(DBException& e){
cout<<"caught"<<e.what()<<endl;
}
return 0;

}

效果如下:

 

推荐阅读