mongocxx官網地址 http://mongocxx.org/?jmp=docs html
本文的安裝版本是:mongocxx-r3.2.0.tar.gz 。linux
參考文檔安裝過程 http://mongocxx.org/mongocxx-v3/installation/ 。根據文檔過程安裝,因爲系統不一致,可能會遇到各類問題,筆者就一路坎坷。ios
Linux系統信息:Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64 GNU/Linuxc++
mongocxx是基於mongo-c-driver的,因此須要先下載安裝最新版本的 mongo-c-driver。此時最新版本是 1.13.0.tar.gz 。git
參考連接:http://mongoc.org/libmongoc/current/installing.html 。本文全部內容都是採用源碼安裝的方式。github
根據文檔要求,cmake須要是3.2或以後的版本。camke --version發現系統的cmake還停留在2.x版本。先升級一下cmake。mongodb
wget https://cmake.org/files/v3.12/cmake-3.12.3.tar.gz tar -xzf cmake-3.12.3.tar.gz
cd cmake-3.12.3 ./configure sudo make && make install cmake --version
其中,cmake --version檢查一下最終的cmake版本。shell
mongo-c-driver的cmake過程:json
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.13.0/mongo-c-driver-1.13.0.tar.gz tar -xzf mongo-c-driver-1.13.0.tar.gz
mkdir cmake-build
cd cmake-build cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_BUILD_TYPE=Release ..
NOTE:這裏要特別注意cmake的輸出,會提示你係統還缺乏什麼庫(NOT FOUND),請不要無視。筆者的mongo-c-driver就安裝了兩遍。vim
例如,openssl和snappy沒有安裝正確。雖然最終cmake和make不會報錯,可是最後面用mongocxx的時候會提示ssl錯誤,不能鏈接mongodb。
附上cmake的部分輸出:
-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR) -- Searching for sasl/sasl.h -- Found in /usr/include -- Searching for libsasl2 -- Found /usr/lib/x86_64-linux-gnu/libsasl2.so -- Detected parameters: accept (int, struct sockaddr *, socklen_t *) -- Searching for compression library header snappy-c.h -- Not found (specify -DCMAKE_INCLUDE_PATH=/path/to/snappy/include for Snappy compression) -- No ICU library found, SASLPrep disabled for SCRAM-SHA-256 authentication. -- If ICU is installed in a non-standard directory, define ICU_ROOT as the ICU installation path. -- SSL disabled -- Compiling against Cyrus SASL -- Configuring done -- Generating done -- Build files have been written to: /home/yyln2745/studyplace/mongo-c-driver-1.13.0/cmake-build
若是SSL disabled的話,後面你寫的代碼應該會報錯:The SCRAM_SHA_1 authentication mechanism requires libmongoc built with ENABLE_SSL: generic server error 。
先肯定是否安裝了openssl。直接在shell輸入openssl命令,或者查看目錄:/usr/local/ssl 是否有東西。肯定沒有則須要安裝一下了。
export OPENSSL_ROOT_DIR=/usr/local/ssl export OPENSSL_CRYPTO_LIBRARY=/usr/local/ssl/lib export OPENSSL_INCLUDE_DIR=/usr/local/ssl/include/
snappy用來壓縮的。雖然不知道不安裝會有什麼問題,可是不想像SSL同樣,遇到問題了再回頭來修,將上面cmake提到的not found的都解決。安裝命令:
wget https://github.com/google/snappy/tarball/master tar -xzf master cd google-snappy-ea660b5/cmake cmake ..
// 暫停一下 make sudo make install
在make前,咱們須要先編輯一下CMakeCache.txt,修改CMAKE_CXX_FLAGS:STRING=-fPIC 。爲何要加 -fPIC 呢?由於不這樣的話,待會mongo-c-driver執行make時,應該會報如下錯誤:
/usr/bin/ld: /usr/local/lib/libsnappy.a(snappy.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC /usr/local/lib/libsnappy.a: error adding symbols: Bad value collect2: error: ld returned 1 exit status src/libmongoc/CMakeFiles/mongoc_shared.dir/build.make:1277: recipe for target 'src/libmongoc/libmongoc-1.0.so.0.0.0' failed make[2]: *** [src/libmongoc/libmongoc-1.0.so.0.0.0] Error 1 CMakeFiles/Makefile2:1678: recipe for target 'src/libmongoc/CMakeFiles/mongoc_shared.dir/all' failed make[1]: *** [src/libmongoc/CMakeFiles/mongoc_shared.dir/all] Error 2 Makefile:151: recipe for target 'all' failed make: *** [all] Error 2
關於更多 fPIC 的知識,可另行學習,建議瞭解一下。
wget http://download.icu-project.org/files/icu4c/4.2.1/icu4c-4_2_1-src.tgz tar -xzf icu4c-4_2_1-src.tgz cd source ./configure make sudo make install
最後一步,接着make & make install mongo-c-driver。
make sudo make install
若是還出現以下錯誤:
[66%]Linking C executable test-libmongoc /usr/bin/ld: /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): undefined reference to symbol 'dlclose@@GLIBC_2.2.5' //lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status src/libmongoc/CMakeFiles/test-libmongoc.dir/build.make:1563: recipe for target 'src/libmongoc/test-libmongoc' failed make[2]: *** [src/libmongoc/test-libmongoc] Error 1 CMakeFiles/Makefile2:1376: recipe for target 'src/libmongoc/CMakeFiles/test-libmongoc.dir/all' failed make[1]: *** [src/libmongoc/CMakeFiles/test-libmongoc.dir/all] Error 2 Makefile:151: recipe for target 'all' failed make: *** [all] Error 2
這是mongo-c-driver測試樣例編譯錯誤,咱們能夠選擇直接幹掉,方法爲編輯mongo-c-driver的CMakeCache.txt文件,修改ENABLE_TESTS:BOOL=OFF,或者在cmake的時候修改,命令以下,注意TEST後面有S:
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTS=OFF ..
wget -OL https://github.com/mongodb/mongo-cxx-driver/archive/r3.3.1.tar.gz tar -xzf r3.3.1.tar.gz cd mongo-cxx-driver-r3.3.1/build cmake -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_MNMLSTC=1 -DCMAKE_INSTALL_PREFIX=/usr/local .. make EP_mnmlstc_core make
sudo make install
其中,/usr/local是與前面的mongodb c driver同目錄, DBSONCXX_POLY_USE_MNMLSTC=1 or DBSONCXX_POLY_USE_BOOST=1
這裏問題不大。
測試代碼以下:
#include <iostream> #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> int main(int, char**) { mongocxx::instance inst{};
//下面uri 請替換成真實數值。 mongocxx::uri yace_uri{"mongodb://test_username:test_pwd@test_ip:test_port/?authSource=test_db"}; mongocxx::options::client client_options; if (yace_uri.ssl()) { std::cout << "ssl-----------\n"; } else { std::cout << "no-ssl\n"; } mongocxx::client conn{yace_uri}; mongocxx::database db = conn["test_db"]; mongocxx::collection gift_test = db["gift_test"]; bsoncxx::stdx::optional<bsoncxx::document::value> res = gift_test.find_one({}); if (res) std::cout << bsoncxx::to_json(*res) << std::endl; return 0; }
編譯命令:
c++ --std=c++11 test.cpp \ -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/libmongoc-1.0 \ -I/usr/local/include/bsoncxx/v_noabi -I/usr/local/include/libbson-1.0 \ -L/usr/local/lib -lmongocxx -lbsoncxx -Wl,-rpath,/usr/local/lib
添加「-Wl,-rpath,/usr/local/lib」 是由於運行時報錯:error while loading shared libraries: libmongocxx.so._noabi。
參考連接:
https://jira.mongodb.org/browse/CXX-1425 for preior question. http://blog.51cto.com/elephantliu/563298 https://blog.csdn.net/furzoom/article/details/70843664 https://stackoverflow.com/questions/6562403/i-dont-understand-wl-rpath-wl
本文介紹了mongocxx的安裝過程,主要記錄其中可能遇到的問題和解決過程。