LoginSignup
2
1

More than 5 years have passed since last update.

MongoDB C++ Driverのビルド(windows)

Last updated at Posted at 2019-03-08

mongoDBをC++のアプリから使うためのライブラリであるmongocxxbsoncxxをビルドしてインストールする。
手順は公式を元に進めた。

環境

  • windows8.1(64bit)
  • Visual Studio Community 2017
  • cmake 3.9.0

手順

mongoDB C Driver

mongocxx driverはmongoDB C Driverに依存しているのでまずそちらをインストールする。方法は公式を参考にした。筆者も別に記事を書いている。
ここではC:\mongo-c-driverbinincludeなどが入るようにインストールされたと想定する。

Boost

mongocxx driverはC++17の機能を使っている。C++17を使わずにコンパイルするにはwindowsではBoostを使うとのこと。
Boostのインストールは別の記事に書く予定。(※ここに書いた)
ここではC:\local\boost_1_69_0includelibが入るようにインストールしたと想定する。

mongocxx

最終的にインストールされるものはヘッダファイルや.dll
適当な場所にソースをクローンする

powershell
git clone https://github.com/mongodb/mongo-cxx-driver.git --branch releases/stable --depth 1
cd mongo-cxx-driver/build

VisualStudio2017でビルドするように設定

  • ここでは最終的にライブラリがC:\mongo-cxx-driverにインストールされる。
  • DBOOST_ROOTに指定するBoostのパスに注意。includeの中にあるboost-1_69を指定する。
  • ちなみに最初VisualStudio2015でやってみたが、No CMAKE_CXX_COMPILER could be found. となって失敗。C++がインストールされていないということで再インストールするも上手くいかなかった。
powershell
cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver -DCMAKE_PREFIX_PATH=C:\mongo-c-driver -DBOOST_ROOT=C:\local\boost_1_69_0\include\boost-1_69 ..

ビルド

powershell
msbuild.exe ALL_BUILD.vcxproj

75 個の警告
0 エラー
経過時間 00:05:18.93

警告は

warning C4819: ファイルは、現在のコード ページ (932) で表示できない文字を含んでいます。データの損失を防ぐために、ファイルを Unicode 形式で保存してください。

みたいな感じだった。未解決だがそのまま進めた。
インストール

powershell
msbuild.exe INSTALL.vcxproj

ビルドに成功しました。
0 個の警告
0 エラー
経過時間 00:00:10.03

以上でインストールされた。

インストールされたか検証

プロジェクト作成

適当な場所にVisualC++のwin32コンソールアプリケーションのプロジェクトを作る。

筆者は知らなかったが、こちらによると.dllは、

  1. アプリケーション(*.exe)と同じフォルダ
  2. カレントディレクトリ
  3. システムディレクトリ(C:\Windows\System32 など)
  4. 16Bitシステムディレクトリ(C:\Windows\System など)
  5. Windowsディレクトリ(C:\Windows など)
  6. PATH環境変数に列挙されているディレクトリ

のような場所に置かないと認識してくれない。
今回は.vcprojがあるフォルダと同じ階層に必要な.dllを入れた。
コピー元のフォルダと必要なファイルは次のとおり。

C:\mongo-cxx-driver\bin
bsoncxx.dll
mongocxx.dll

C:\mongo-cxx-driver\lib
bsoncxx.lib
mongocxx.lib

C:\mongo-c-driver\bin
libbson-1.0.dll
libmongoc-1.0.dll

binで共通なファイル
concrt140.dll
msvcp140.dll
vcruntime140.dll

.libはスタティックリンクライブラリらしい。ないとエラーになる)

プロジェクト設定

VisualStudioの[プロジェクト->プロパティ]から、次を設定する。
環境
PATH=c:/mongo-c-driver/bin
追加のライブラリディレクトリ
C:\mongo-cxx-driver\lib
C:\mongo-c-driver\lib
C:\local\boost_1_69_0\lib
追加のインクルードディレクトリ
C:\mongo-cxx-driver\include\mongocxx\v_noabi
C:\mongo-cxx-driver\include\bsoncxx\v_noabi
C:\local\boost_1_69_0\include\boost-1_69
C:\mongo-c-driver\include\libmongoc-1.0
C:\mongo-c-driver\include\libbson-1.0
追加の依存ファイル
bsoncxx.lib
mongocxx.lib
mongoc-1.0.lib
bson-1.0.lib

コード

testdbtestcollectionを作成して、hello:worldというドキュメントをインサートするコード。
実行前にmongoDBがインストールされていて、デフォルト(localhost:27017)で起動していること。

ConsoleApplication.cpp
#include "stdafx.h"
#include <iostream>

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

#include <mongoc/mongoc.h>

int main(int, char**) {
    mongocxx::instance inst{};
    mongocxx::client conn{ mongocxx::uri{} };

    bsoncxx::builder::stream::document document{};

    auto collection = conn["testdb"]["testcollection"];
    document << "hello" << "world";

    collection.insert_one(document.view());
    auto cursor = collection.find({});

    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }
    Sleep(10000);
}

実行

mongoDBの起動はこんな感じ(mongodコマンドのある場所(C:\Program Files\MongoDB\Server\4.0\binなど)にパスが通っている)

powershell
mongod --dbpath c:\data\db

VisualStudioで上記の検証コードを実行

{ "_id" : { "$oid" : "5c7f9d78cf5f0000e9004d62" }, "hello" : "world" }

あとはMongoDB Compass等で確かにインサートされたか確認する。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1