[轉]MongoDB c++驅動安裝與使用

  1. 安裝
    • 獲取源碼:git clone https://github.com/mongodb/mongo-cxx-driver.git,解壓
    • 安裝編譯工具scons:yum install -y scons
    • 編譯:進入mongo-cxx-driver目錄,執行:scons --prefix=/home/work/mongo/ --sharedclient install
    • 驅動已被安裝在/home/work/mongo中
  2. 使用
    • 編譯示例程序
      #include <cstdlib> #include <iostream> #include "mongo/client/dbclient.h" // for the driver void run() { mongo::DBClientConnection c; c.connect("localhost"); } int main() { mongo::client::initialize(); try { run(); std::cout << "connected ok" << std::endl; } catch( const mongo::DBException &e ) { std::cout << "caught " << e.what() << std::endl; } return EXIT_SUCCESS; }
    • gcc tutorial.cpp -I./mongo/include -L./lib/ -L./mongo/lib -lmongoclient -Wl,-rpath=./lib/ -o tutorial
    • 上面的編譯選項中,mongo、lib與tutorial.cpp位於同一目錄,mongo便是第一步中指定的安裝目錄,lib存放其餘依賴庫,如boost。

  更多文章,請關注零一積流ios

相關文章
相關標籤/搜索