1、数据库版本:3.4.9
c++库版本要求:mongocxx 3.1.x
2、编译C++库
第一步:需要先安装MongoDB C driver(mongocxx driver builds on top of the MongoDB C driver)
参考:http://mongoc.org/libmongoc/current/installing.html
mongocxx-3.1.x要求libmongoc 1.5.0或者更高的版本
检查安装依赖库:认证或者SSL方式连接MongoDB需要OpenSSL
sudo apt-get install pkg-config libssl-dev libsasl2-dev
下载mongo-c-driver:https://github.com/mongodb/mongo-c-driver/releases
第二步:
mongocxx driver 用到了C++17的一些特性:std::optional std::string_view,编译mongocxx需要选择下面两个的其中一个:
MNMLSTC/core
-DBSONCXX_POLY_USE_MNMLSTC=1
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig cmake -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_MNMLSTC=1 -DCMAKE_INSTALL_PREFIX=/usr/local ..
Boost
-DBSONCXX_POLY_USE_BOOST=1
cmake:
-DBSONCXX_POLY_USE_STD_EXPERIMENTAL=1(if toolchain's standard library provides optional and string_view in the namespace std::experimental)
第三步:
使用Boost方式:cmake -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_BOOST=1 -DCMAKE_INSTALL_PREFIX=/usr/local -DBOOST_ROOT=/usr/local/boost_1_65_0
第四步:
make
sudo make install
3、按照官网上的例子进行测试
可能会出现libmongocxx.so._noabi没找到的问题
sudo gedit /etc/ld.so.conf
末行添加:/usr/local/lib
sudo ldconfig -v
再次执行生成的可执行文件,正常运行
c++库版本要求:mongocxx 3.1.x
2、编译C++库
参考:https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/
- 需要一个支持c++11的编译器(gcc[4.8/5.4],clang,visual studio)
- CMake版本为3.2或者以上
- boost(我这里编译的1.65)
第一步:需要先安装MongoDB C driver(mongocxx driver builds on top of the MongoDB C driver)
参考:http://mongoc.org/libmongoc/current/installing.html
mongocxx-3.1.x要求libmongoc 1.5.0或者更高的版本
检查安装依赖库:认证或者SSL方式连接MongoDB需要OpenSSL
sudo apt-get install pkg-config libssl-dev libsasl2-dev
下载mongo-c-driver:https://github.com/mongodb/mongo-c-driver/releases
tar -xzvf mongo-c-driver-1.7.0.tar.gz
cd mongo-c-driver-1.7.0
./configure --disable-automatic-init-and-cleanup --enable-static
make
sudo make install
第二步:
mongocxx driver 用到了C++17的一些特性:std::optional std::string_view,编译mongocxx需要选择下面两个的其中一个:
MNMLSTC/core
-DBSONCXX_POLY_USE_MNMLSTC=1
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig cmake -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_MNMLSTC=1 -DCMAKE_INSTALL_PREFIX=/usr/local ..
Boost
-DBSONCXX_POLY_USE_BOOST=1
cmake:
-DBSONCXX_POLY_USE_STD_EXPERIMENTAL=1(if toolchain's standard library provides optional and string_view in the namespace std::experimental)
第三步:
使用Boost方式:cmake -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_BOOST=1 -DCMAKE_INSTALL_PREFIX=/usr/local -DBOOST_ROOT=/usr/local/boost_1_65_0
第四步:
make
sudo make install
3、按照官网上的例子进行测试
可能会出现libmongocxx.so._noabi没找到的问题
sudo gedit /etc/ld.so.conf
末行添加:/usr/local/lib
sudo ldconfig -v
再次执行生成的可执行文件,正常运行