编译安装MongoDB C++ Driver (win8.1 vs2013)

在C++中调用mongodb的库函数需要安装mongodb的c++driver,需要自己编译,(自己搞了一天半 =_=''' )
官网Build MongoDB From Source 说To build MongoDB, you will need:

  • Visual Studio 2013 Update 2 or newer(GCC 4.8.2 or newer,Clang 3.4 (or Apple XCode 5.1.1 Clang) or newer)
  • Python 2.7
  • SCons 2.3

还需要boost,mongo-cxx-driver源码

我编译是在win8.1+vs2013+python2.7.9+scons2.3.4+boost1_57_0

步骤

  1. 已安装VS2013 ,python2.7.9 并添加到环境变量中。
  2. 安装scons:下载好后,setup.py install
  3. MongoDB C++驱动依赖Boost库,我是直接下载编译好的Boost文件直接安装,节约了时间;
  4. 将下载好的mongodb c++ driver源码解压,
  5. cmd->cd 到解压目录下
  6. scons install --dbg=on --32 --dynamic-windows --sharedclient --cpppath=D:oost_1_57_0 --libpath=D:oost_1_57_0lib32-msvc-12.0(后面加boost路径)
  7. 开始编译,结束后会在目录下生成一个build文件夹:其中有个install文件夹

    install中的东西就是我们想要的。

测试:
将boost和install中的lib路径添加到环境变量中(我是D:install_mongodblib;)
vs2013新建空项目(要包好boost和生成的install中的头文件目录,库目录)

#include <iostream>
#include <cstdlib>
#include <winsock2.h>

#include "mongoclientdbclient.h"

using namespace std;

void run() {
	mongo::DBClientConnection c;
	c.connect("localhost");
}

int main() {
	mongo::client::initialize();
	try {
		run();
		std::cout << "connected ok" << std::endl;
	}
	catch (const mongo::DBException &e) {
		std::cout << "caught " << e.what() << std::endl;
	}
	return 1;
}


不能连接是还没打开mongodb server ,将下载mongodb安装后执行mongod.exe就可以了

以上参考了很多网上的资料:
http://www.cnblogs.com/ym123/p/4282043.html
http://blog.csdn.net/baiwfg2/article/details/38044401
http://blog.csdn.net/lyq240919525/article/details/40452737

原文地址:https://www.cnblogs.com/iois/p/4320166.html