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\includeD:\temp\mongo-c-driver\lib 两个文件夹复制到项目目录下,再将include目录下的两个文件夹改名:libbson-1.0 –> bsonlibmongoc-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

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性