一、 安裝環境和准備工作
- 安裝環境是win10,64位的系統。
- 電腦安裝的是vs2017 Professional版本
- 請先在電腦上安裝cmake。(https://cmake.org/download/)
注意:請選擇適合您系統的版本。
例如,我是64位的系統,我選擇的是 cmake-3.17.1-win64-x64.msi。
二、 官方的安裝介紹
- 官方文章1 http://mongoc.org/libmongoc/current/installing.html#
- 官方文章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; }