码迷,mamicode.com
首页 > 其他好文 > 详细

mongocxx-driver编译安装

时间:2018-06-23 01:33:42      阅读:682      评论:0      收藏:0      [点我收藏+]

标签:ibm   href   test   prefix   make   依赖项   init   evel   toc   

1. 确保安装epel

yum install -y epel-release

2. 按照《CentOS7.2部署node-mapnik》一文中的步骤,手动安装 gcc-6.2.0 和 boost-1.65.1 

3. 按照官方文档安装MongoDB,请注意开启防火墙端口和设置SELinux相关选项,(本示例中创建了数据库 gis , dbOwner 用户名为 root ,密码为 111111 )

4.  安装编译工具及依赖项

yum install -y automake autoconf libtool cmake3 openssl-devel unzip

5. 下载解压 mongo-c-driver 源代码

wget https://github.com/mongodb/mongo-c-driver/releases/download/1.10.0/mongo-c-driver-1.10.0.tar.gz

tar -xzvf mongo-c-driver-1.10.0.tar.gz

cd mongo-c-driver-1.10.0

mkdir cmake-build && cd cmake-build

6. 编译安装

#注意后面是两个..(点号)
cmake3 -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..

make -j4

make install

ldconfig

7. 下载并解压 mongo-cxx-driver 

wget https://github.com/mongodb/mongo-cxx-driver/archive/r3.2.0.zip

unzip r3.2.0.zip

cd mongo-cxx-driver-r3.2.0

8. 编译安装

cd build

cmake3 -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_BOOST=1 -DCMAKE_INSTALL_PREFIX=/usr/local ..

make -j8

make install

9. 添加 PKG_CONFIG_PATH 环境变量,使用 vim /etc/profile 打开文件,在最下面输入 export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig ,然后 source /etc/profile 应用环境变量

技术分享图片

10. 测试libmongocxx库

10.1 登录mongodb mongo -u <user> -p <password> --authenticationDatabase <dbname> 

10.2 插入测试数据 db.points.insert({"hello": "world!"}) 

技术分享图片

10.3 输入c++测试代码

#include <iostream>

#include <bsoncxx/json.hpp>

#include <mongocxx/instance.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/cursor.hpp>

int main(int, char**)
{
  mongocxx::instance instance{};
  mongocxx::client client{ mongocxx::uri {"mongodb://root:111111@192.168.1.67:27017/?authSource=gis"}};

  auto collection = client["gis"]["points"];

  auto cursor = collection.find({});

  for(auto &&doc : cursor)
  {
    std::cout << bsoncxx::to_json(doc) << std::endl;
  }
}

10.4 编译c++代码,生成测试程序

g++ -std=c++11 test.cpp -o test $(pkg-config --cflags --libs --libmongocxx)

10.5 运行测试程序 ./test 

运行结果如下:

技术分享图片

 

mongocxx-driver编译安装

标签:ibm   href   test   prefix   make   依赖项   init   evel   toc   

原文地址:https://www.cnblogs.com/think8848/p/9215439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!