zoukankan      html  css  js  c++  java
  • Mongo-cxx-driver驱动构建和安装

    一、 安装环境和准备工作

    二、 官方的安装介绍

    1. 官方文章1 http://mongoc.org/libmongoc/current/installing.html#
    2. 官方文章2 http://mongocxx.org/mongocxx-v3/installation/
     注意:请注意选择的Mongo-cxx-drvier 版本

     

    三、下载mongo-cxx-driver

    1. 方法1:在release页面找合适的release版本,并下载。
    release版本页面地址: https://github.com/mongodb/mongo-cxx-driver/releases

      

      

     2. 方法2:在github找到mongo-cxx-driver的工程

    github 的 mongo-cxx-driver工程地址:https://github.com/mongodb/mongo-cxx-driver

      

      特别注意,不要直接下载master版本。

    要点击master,选择一个release版本。这里,我们选择的是V3.5的版本。然后下载下来。
    备注:当前时间,2020年4月26日,最新版本的Mongo-cxx-driver是V3.5版本,它需要的Mongo-c-driver最低版本是V1.15。前面,我们已经完成了mongo-c-driver V1.15版本的构建和安装。官方的参考链接里面,也特别提到了版本号的对应信息。(这些细节不注意,会出现很多奇怪错误)

    四、 开始构建和安装

    以我们下载的 mongo-cxx-driver-releases-v3.5.zip 文件为例进行介绍:

    1. 先解压 mongo-cxx-driver-releases-v3.5.zip 文件夹
    然后在解压后的 mongo-cxx-driver-releases-v3.5 文件夹中,打开cmd窗口。(或者打开cmd窗口,进入mongo-cxx-driver-releases-v3.5目录)


    2. 对当前文件夹,执行git初始化命令(构建过程需要git下载一些东西,如果没有这个步骤会报错)

    git init 

    3. 在mongo-cxx-driver-releases-v3.5中创建一个准备构建的文件夹:

    mkdir cmake-build

    4. 进入cmake-build文件夹

    cd cmake-build

    5. 配置驱动环境 

    cmake .. -G "Visual Studio 15 2017 Win64" "-DCMAKE_CXX_STANDARD=17" "-DCMAKE_BUILD_TYPE=Release" "-DBUILD_SHARED_AND_STATIC_LIBS=ON" -DCMAKE_CXX_FLAGS="/Zc:__cplusplus" "-DBOOST_ROOT=C:localoost_1_72_0" "-DCMAKE_PREFIX_PATH=C:mongo-c-driver" "-DCMAKE_INSTALL_PREFIX=C:mongo-cxx-driver" "-DBUILD_VERSION=1.0.2"   

    补充说明:

    C:localoost_1_72_0 是你的boost库安装目录(默认是这个目录)
    C:mongo-c-driver 是你安装的mongo-c-driver驱动目录(默认也是这个目录)
    C:mongo-cxx-driver 是你mongo-cxx-driver要安装的目录(不建议修改)
    -DBUILD_VERSION=1.0.2 是一个版本号,没有这个,构建的时候会有错误提示。这个可以随意指定版本号。


    6. 将msbuild.exe 配置到电脑的环境变量中
    将msbuild.exe的路径,追加到Path的环境变量后面。(如果不懂windows环境变量配置,请自行百度了解一下)。
    在我的电脑上,msbuild.exe 在我的电脑是 C:Program Files (x86)Microsoft Visual Studio2017ProfessionalMSBuild15.0Binamd64


    7. 编译和安装驱动

    msbuild.exe ALL_BUILD.vcxproj
    msbuild.exe INSTALL.vcxproj
    msbuild.exe generate_uninstalluninstall.vcxproj

    执行完第7步命令之后,在电脑的”C:mongo-cxx-driver”目录下面就会生成若干文件了。Mongon-cxx-driver驱动就安装在这里。
    在第7步,也可以用vs2017打开我们刚才构建的工程。右键选中ALL_BUILD,然后点击生成。生成完成之后,右键选中 INSTALL,然后点击生成。就安装成功了。

      

    恭喜你驱动安装成功!!!!

    如果安装失败了,请重新按照文档讲解来一步一步操作。

    特别注意下载的工程的版本。

    你也可以再回过头看看官方介绍的安装步骤,看看哪个地方疏漏了。


    五、 跑一个案例

    1. 创建工程:
    用vs2017新建一个控制台应用程序,并把win32环境改为win64.


    2. 设置属性面板
     2.1【c/c++】【常规】,在【附加包含目录】,添加bsoncxx和mongocxx的头文件:

    C:mongo-cxx-driverincludemongocxxv_noabi
    C:mongo-cxx-driverincludesoncxxv_noabi

    我们假设你的mongo-cxx-driver是安装在了C:mongo-cxx-driver 目录下面。

     2.2【链接器】【常规】,在【附加库目录】,添加库文件目录:

    C:mongo-cxx-driverlibsoncxx.lib
    C:mongo-cxx-driverlibmongocxx.lib

     2.3【链接器】【输入】,在【附加依赖项】中,添加库文件目录:

    C:mongo-cxx-driverlibsoncxx.lib
    C:mongo-cxx-driverlibmongocxx.lib

      

     2.4【调试】,在【环境】中添加dll的路径

    PATH=C:/mongo-cxx-driver/bin;C:mongo-c-driverin

    要真正运行程序,还需要将dll放在可执行路径中,这里我可以通过设置PATH,在工程中运行我们的程序。mongocxx需要mongoc的若干库文件。

      

     2.5预处理器

    _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS

     2.6语言环境设置为C++17

     


     2.7示例代码:

    // FirstMongoCXXProject.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    //
    #include <iostream>
    #include <sstream>
    #include <thread>
    #include <vector>
    #include <cstdint>
    #include <mongocxx/index_model.hpp>
    #include <bsoncxx/builder/stream/document.hpp>
    #include <bsoncxx/builder/stream/helpers.hpp>
    #include <bsoncxx/builder/basic/helpers.hpp>
    #include <mongocxx/client.hpp>
    #include <mongocxx/instance.hpp>
    using bsoncxx::builder::stream::close_array;
    using bsoncxx::builder::stream::close_document;
    using bsoncxx::builder::stream::document;
    using bsoncxx::builder::stream::finalize;
    using bsoncxx::builder::stream::open_array;
    using bsoncxx::builder::stream::open_document;
    int main(int, char**) {
    //===================example:插入一个Json
    mongocxx::instance instance{}; // This should be done only once.
    mongocxx::client client{ mongocxx::uri{} };
    mongocxx::database db = client["mydb"];
    mongocxx::collection coll = db["test"];
    auto builder = bsoncxx::builder::stream::document{};
    bsoncxx::document::value doc_value = builder
    << "name" << "MongoDB"
    << "type" << "database"
    << "count" << 1
    << "versions" << bsoncxx::builder::stream::open_array
    << "v3.2" << "v3.0" << "v2.6"
    << close_array
    << "info" << bsoncxx::builder::stream::open_document
    << "x" << 203
    << "y" << 102
    << bsoncxx::builder::stream::close_document
    << bsoncxx::builder::stream::finalize;
    bsoncxx::document::view view = doc_value.view();
    bsoncxx::document::element element = view["name"];
    if (element.type() != bsoncxx::type::k_utf8) {
    // Error
    }
    std::string name = element.get_utf8().value.data(); //stdx::string_view 类型的 data()方法,可以转为字符串指针
    bsoncxx::stdx::optional<mongocxx::result::insert_one> result =
    coll.insert_one(view);
    }
  • 相关阅读:
    Error:dojo.data.ItemFileWriteStore:Invalid item argument
    TypeError:_12.store.query is not a function
    Error:dijit.tree.TreeStoreModel:root query returned 0 items
    dijit.byId("grid") is undefined
    ORA-00600:internal error code,arguments:[keltnfy-idmlnit],[46],[1],[],[],[],[],[]
    gc cr block busy
    gc buffer busy acquire
    gc cr request
    LVS+Keepalived实现高可用集群
    关于函数授权问题
  • 原文地址:https://www.cnblogs.com/music-liang/p/12780094.html
Copyright © 2011-2022 走看看