@breakerthb
2016-12-01T07:52:37.000000Z
字数 1748
阅读 1815
Linux
DB
https://github.com/mongodb/mongo-c-driver
https://github.com/mongodb/mongo-c-driver/releases/download/1.3.0/mongo-c-driver-1.3.0.tar.gz
$ tar xzf mongo-c-driver-1.3.0.tar.gz
$ cd mongo-c-driver-1.3.0
$ ./configure
$ make
$ sudo make install
$ yum install pcre
下载:http://ncu.dl.sourceforge.net/project/scons/scons/2.5.1/scons-2.5.1.tar.gz
$ cd scons-2.5.1
$ python setup.py install
$ yum install boost*
下载:http://dl.MongoDB.org/dl/cxx-driver/
可以直接下载:
$ wget http://downloads.mongodb.org/cxx-driver/mongodb-linux-x86_64-v2.2-latest.tgz
这里下载最新的cxx-driver/mongodb-Linux-x86_64-v2.0-latest.tgz版本
$ tar zxf mongodb-linux-x86_64-v2.2-latest.tgz
$ cd mongo-cxx-driver-v2.2
$ scons
$ scons install
$ ldconfig /usr/local/lib
PS:如果出现下面的错误:
error: #error Compiling Filesystem version 3 file with BOOST_FILESYSTEM_VERSION defined != 3
这是因为 src/mongo/pch.h 文件中
#define BOOST_FILESYSTEM_VERSION 2
但在 /usr/local/include/boost/filesystem/config.hpp中:
# if defined(BOOST_FILESYSTEM_VERSION) && BOOST_FILESYSTEM_VERSION != 3
# error Compiling Filesystem version 3 file with BOOST_FILESYSTEM_VERSION defined != 3
# endif
这两个宏不匹配导致。
解决方法:
src/mongo/pch.h文件中修改如下:
#define BOOST_FILESYSTEM_VERSION 3
重新编译即可。
ref:http://stackoverflow.com/questions/12105202/mongodb-c-driver-install-on-linux
安装后,在/usr/local/lib目录下会看到文件:libmongoclient.a
头文件目录在:/usr/local/include
新建文件main.cpp
#include <iostream>
#include "mongo/client/dbclient.h"
using namespace std;
using namespace mongo;
void run() {
DBClientConnection c;
c.connect("localhost"); //add port,c.connect("localhost:27017")
}
int main(void)
{
try {
run();
cout<<"connected ok"<<endl;
}catch(DBException& e){
cout<<"caught"<<e.what()<<endl;
}
return 0;
}
编译:
$ g++ main.cpp -I/usr/local/include -lmongoclient -lboost_thread -lboost_filesystem -lboost_thread-mt -lpthread
运行:
$ ./a.out