Visual Studio 2017 编译 mongo-c-driver库
Visual Studio 2017 编译 mongo-c-driver库
运行环境:
🟢 操作系统:windows 10 64
🟢 IDE: Visual Studio 2017
🟢 编译工具:CMake 3.16.2 windows 64
🟢 MongoDB Server:MongoDB 5.0.13 windows 64
🟢 MongoDB 管理工具:MongoDB Compass 1.33.1 windows 64
一、mongo-c-driver库的编译
❤ 第一步:下载
GitHub下载地址:https://github.com/mongodb/mongo-c-driver/archive/refs/heads/master.zip
❤ 第二步:CMake
解压 mongo-c-driver-master.zip
,进入mongo-c-driver-master
文件夹并创建文件夹:build-vs2017
打开CMake,选择编译文件夹和输出文件夹
(注意:必须设置版本号,否则可能编译失败)
❤ 第三步:Visual Studio 2017 编译
进入:mongo-c-driver-master\build-2017
打开 mongo-c-driver.sln
工程文件
在:D:\temp\mongo-c-driver
目录下生成编译结果
二、mongo-c-driver库的简单使用
❤ 启动MongoDB Server,打开MongoDB Compass工具
❤ Visual Studio 2017新建项目,配置项目环境:
将D:\temp\mongo-c-driver\include
和 D:\temp\mongo-c-driver\lib
两个文件夹复制到项目目录下,再将include
目录下的两个文件夹改名:libbson-1.0
–> bson
、libmongoc-1.0
–> mongoc
包含头文件目录
包含库目录
包含具体库文件
❤ 创建源文件,复制官方例子: http://mongoc.org/libmongoc/current/tutorial.html#include-and-link-libmongoc-in-your-c-program
#include <mongoc/mongoc.h>
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 MongoDB 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 parse 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;
}
编译、运行…(第一次会出错,原因:找不到.dll文件
)
将D:\temp\mongo-c-driver\bin
目录下的文件全部复制到Mongodb_Client_Project\Debug
下,即复制到.exe文件所在路径
再次编译、运行…
❤ MongocDB Compass工具查看结果
原文地址:http://www.cnblogs.com/caojun97/p/16898930.html