Mongo-c-driver驱动构建和安装

一、     安装环境和准备工作

  1. 安装环境是win10,64位的系统。
  2. 电脑安装的是vs2017 Professional版本
  3. 请先在电脑上安装cmake。(https://cmake.org/download/

    注意:请选择适合您系统的版本。

例如,我是64位的系统,我选择的是 cmake-3.17.1-win64-x64.msi。

二、     官方的安装介绍

  1. 官方文章1   http://mongoc.org/libmongoc/current/installing.html#
  2. 官方文章2   http://mongocxx.org/mongocxx-v3/installation/

    注意:请注意您选择的Mongo-c-drvier 版本

 

 三、     下载Mongo-C-Driver

方法1:在release页面找合适的release版本,并下载

release版本页面地址:https://github.com/mongodb/mongo-c-driver/releases

 

 

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

Github  mongo-c-driver地址:https://github.com/mongodb/mongo-c-driver/tree/master

    特别注意,不要直接下载master版本。要点击master,选择一个release版本。例如,这里选择的是r1.15。然后下载下来。

备注:

当前时间,2020年4月26日,最新版本的Mongo-cxx-driver是V3.5版本,它需要的Mongo-c-driver最低版本是V1.15。所以,我们至少要下载V1.15的Mongo-c-driver的工程来构建(build and install)。参考链接里面,官方的文章有特别写明了版本号的对应信息

 

 

 四、     开始构建和安装

以我们下载的 mongo-c-driver-1.15.3.tat.gz 文件为例进行介绍:

 

1.先解压mongo-c-driver-1.15.3.tat.gz 文件

然后在解压后的 mongo-c-driver-1.15.3 文件夹中,打开cmd窗口。(或者打开cmd窗口,进入mongo-c-driver-1.15.3目录)。

 

2.在mongo-c-driver-1.15.3中创建一个准备构建的文件夹:

mkdir   cmake-build

 

3.进入文件夹

cd   cmake-build

 

4.执行构建命令(不要忽略了最后面的2个点

cmake -G "Visual Studio 15 2017 Win64" "-DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver" "-DCMAKE_PREFIX_PATH=C:\mongo-c-driver"  ..  

 

5.将msbuild.exe 配置到电脑的环境变量中

将msbuild.exe的路径,追加到Path的环境变量后面。(如果不懂windows环境变量配置,请自行百度了解一下)。

在我的电脑上,msbuild.exe 在我的电脑是 C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\amd64

 

6.执行编译命令

msbuild.exe  /p:Configuration=RelWithDebInfo ALL_BUILD.vcxproj

 

7.执行安装命令

msbuild.exe  /p:Configuration=RelWithDebInfo INSTALL.vcxproj

 

执行完第7步命令之后,在电脑的”C:\mongo-c-driver”目录下面就会生成若干文件了。Mongo-c-driver驱动就安装在这里。

 

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

如果你安装失败了,请重新再按照讲解步骤来一步一步操作。特别注意下载的mongo-c-driver工程的版本号问题。

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

 

五、     跑一个案例

 

请参考官方的这篇文章:http://mongoc.org/libmongoc/current/visual-studio-guide.html

官方文章讲解的非常详细,我这里把主要步骤概况一下:

 

1.创建工程

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

 

2.     设置属性面板

u  2.1【c/c++】-->【常规】,在【附加包含目录】,添加libbson和libmongoc的头文件:

C:\mongo-c-driver\include\libbson-1.0

C:\mongo-c-driver\include\libmongoc-1.0

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

 

 

u  2.2【链接器】-->【常规】,在【附加库目录】,添加库文件目录:

C:\mongo-c-driver\lib\mongoc-1.0.lib

C:\mongo-c-driver\lib\bson-1.0.lib

 

  

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

C:\mongo-c-driver\lib\mongoc-1.0.lib

C:\mongo-c-driver\lib\bson-1.0.lib

 

  

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

PATH=c:/mongo-c-driver/bin

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

 

 

 u  2.5在你的工程中记得包含头文件

#include <mongoc/mongoc.h>

 

2.6示例代码

#include <iostream>
#include <mongoc/mongoc.h>
//vs2017 环境配置地址
//http://mongoc.org/libmongoc/current/visual-studio-guide.html
int main(int argc, char* argv[])
{
    const char* uri_string = "mongodb://localhost:27017";
    mongoc_uri_t *uri;
    mongoc_client_t *client;
    mongoc_database_t *database;
    mongoc_collection_t *collection;
    bson_t *command, reply, *insert;
    bson_error_t error;
    char *str;
    bool retval;
    /*
    * Required to initialize libmongoc's internals
    */
    mongoc_init();
    /*
    * Optionally get MonogDB URI from command line
    */
    if (argc > 1)
    {
        uri_string = argv[1];
    }
    /*
    * safely create a MongoDB URI object from the given string
    */
    uri = mongoc_uri_new_with_error(uri_string, &error);
    if (!uri) {
        fprintf(stderr,
            "failed to parsse URI: %s \n"
            "error message:      %s \n",
            uri_string,
            error.message);
        return EXIT_FAILURE;
    }
    /*
    * create a new client instance 
    */
    client = mongoc_client_new_from_uri(uri);
    if (!client) {
        return EXIT_FAILURE;
    }
    /*
    * Register the application name so we can track it in the profile logs
    * on the server. This can also be done from the URI(see other examples).
    */
    mongoc_client_set_appname(client, "connect-example");
    /*
    * Get a handle on the database "db_name" and collection "coll_name"
    */
    database = mongoc_client_get_database(client, "db_name");
    collection = mongoc_client_get_collection(client, "db_name", "coll_name");
    /*
    * Do work. This example pings the database, prints the result as JSON and 
    * performs an insert
    */
    command = BCON_NEW("ping", BCON_INT32(1));
    retval = mongoc_client_command_simple(
        client, "admin", command, NULL, &reply, &error);
    if (!retval)
    {
        fprintf(stderr, "%s \n", error.message);
        return EXIT_FAILURE;
    }
    str = bson_as_json(&reply, NULL);
    printf("%s \n", str);
    insert = BCON_NEW("Hello", BCON_UTF8("world"));
    if (!mongoc_collection_insert_one(collection, insert, NULL, NULL, &error)) {
        fprintf(stderr, "%s\n", error.message);
    }
    bson_destroy(insert);
    bson_destroy(&reply);
    bson_destroy(command);
    bson_free(str);
    /*
    * Release our handles and clean up libmongoc
    */
    mongoc_collection_destroy(collection);
    mongoc_database_destroy(database);
    mongoc_uri_destroy(uri);
    mongoc_client_destroy(client);
    mongoc_cleanup();
    return EXIT_SUCCESS;
}

 

posted @ 2020-04-26 13:44  He_LiangLiang  阅读(2931)  评论(0编辑  收藏  举报