一个mongo-c的Demo(CMakeLists.txt编译)

 

关于CMake的入门知识:
https://www.hahack.com/codes/cmake/

 

 

CMakeLists.txt

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)

# 项目信息
project (hello_mongoc2)

# 查找目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)

# Specify the minimum version you require.
find_package (mongoc-1.0 1.7 REQUIRED)

# The "hello_mongoc.c" sample program is shared among four tests.
add_executable (hello_mongoc   ${DIR_SRCS})
target_link_libraries (hello_mongoc PRIVATE mongo::mongoc_shared)


##  添加头文件目录INCLUDE_DIRECTORIES
##它相当于g++选项中的-I参数的作用,也相当于环境变量中增加路径到CPLUS_INCLUDE_PATH变量的作用。
include_directories(../include)

##添加需要链接的库文件目录LINK_DIRECTORIES
##它相当于g++命令的-L选项的作用,也相当于环境变量中增加LD_LIBRARY_PATH的路径的作用。
link_directories("/home/server/third/lib")





## Specify the minimum version you require.
#find_package (mongoc-1.0 1.7 REQUIRED)
#
## The "hello_mongoc.c" sample program is shared among four tests.
#add_executable (hello_mongoc   ${DIR_SRCS})
#target_link_libraries (hello_mongoc PRIVATE mongo::mongoc_static)

 

 

mongoDemo.cpp

#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-05-20 09:34  He_LiangLiang  阅读(378)  评论(0编辑  收藏  举报