Docs Menu
Docs Home
/ / /
C++ ドライバー

プログラムにドライバーを含めてリンクする

このガイドでは、CMax と pkg-config を使用してC++ドライバーをプロジェクトに含める方法を説明します。

このページの例では、C++ドライバーソースコードの view_and_value.cp 例プログラムを使用します。 C++ドライバーのソースコードを取得するには、 ダウンロードとインストール のガイドに従ってください。

次のセクションで、<path-to-mongo-cxx-driver-sources> を、 C++ドライバー ソース ツリーがシステム上にある実際のパスに置き換えます。

このプログラムはMongoDBサーバー上で実行する必要はありません。

このページの例は、C++ドライバー ライブラリのターゲットを参照。 CSpec セクションの例では mongo::bsoncxx_shared ターゲットが使用され、 pkg-config セクションの例では libbsoncxxパッケージが使用されます。プロジェクトのニーズに応じて、次の代替ライブラリ ターゲットを使用できます。

  • CSpec: -mongo::bsoncxx_shared - mongo::mongoccxx_shared - mongo::bsoncxx_static - mongo::mongoccxx_static

  • pkg-config: - libbsoncxx - libmongocxx - libbsoncxx-static - libmongocxx-static

ターゲットの可用性は、特定のインストール方法によって異なります。

C# を使用して、プロジェクトにC++ドライバーを含めることができます。 CSpec は find_package コマンドを提供しています。このコマンドは、インストールがインストールされるとプロジェクトがC++ドライバーを見つけるために使用できます。あるいは、プロジェクトでは、 C++ドライバーをインストールせずに高度な add_subdirectory コマンドを使用できます。

次のセクションでは、エラーが発生した場合は、最初の CSpec コマンドに追加のオプションを指定する必要がある可能性があります。具体的な オプションは、特定の環境によって異なります。ただし、次のオプションは最も一般的な問題に対処します。

  • コンパイラーがデフォルトで少なくともC++17 に設定されない場合は、-DCMAKE_CXX_STANDARD=17 CMax オプションを使用します。

  • ドライバーを非標準のロケーションにインストールした場合は、-DCMAKE_PREFIX_PATH=/<path-to-mongo-cxx-driver-installation> オプションを指定します。 (例: )。

cmake -Bbuild -DCMAKE_CXX_STANDARD=17 -DCMAKE_PREFIX_PATH=/opt/mongodb/cxx-driver

C++ドライバーをインストールしたら、CMax の find_package コマンドを使用してドライバーをプロジェクトと統合できます。

Tip

C++ドライバーをインストールする方法については、次のガイドを参照してください。

find_package コマンドを使用するには、プロジェクトディレクトリに CMakeLists.txtファイルを作成します。次の例では、find_packageを使用する /home/user/project1プロジェクトディレクトリに CMakeLists.txtファイルを作成します。

/host/user/プロジェクト1/CMaxLists.txt
cmake_minimum_required (VERSION 3.15)
project (builder_basic LANGUAGES CXX)
find_package (bsoncxx 4.0 REQUIRED)
add_executable (view_and_value /<path-to-mongo-cxx-driver-sources>/examples/bsoncxx/view_and_value.cpp)
# we need target_include_directories because view_and_value.cpp refers to a common example header
target_include_directories (view_and_value PRIVATE /<path-to-mongo-cxx-driver-sources>)
target_link_libraries (view_and_value PRIVATE mongo::bsoncxx_shared)

次に、以下のコマンドを実行してプロジェクトを構築します。

$ cd /home/user/project1
$ cmake -Bbuild
$ cmake --build build

これらのコマンドは、ビルドプロセスに関する情報を返します。ビルドが完了したら、前の手順で生成された実行可能ファイルを実行します。

$ ./build/view_and_value

出力は次のようになります。

{ "team" : "platforms", "id" : { "$oid" : "66f4be6fef9eb8b9240619f0" }, "members" : [ "tyler", "jason", "drew", "sam", "ernie", "john", "mark", "crystal" ] }
Got key, key = team
Got String!
Got key, key = id
Got ObjectId!
Got key, key = members
Got Array!
array element: tyler
array element: jason
array element: drew
array element: sam
array element: ernie
array element: john
array element: mark
array element: crystal
as expected, we have a team
document has 3 keys.
document keys are:
team
id
members

あるいは、 C++ドライバーをインストールせずに、CMax の add_subdirectory コマンドを使用することもできます。これは高度な手法であり、find_package コマンドとは異なり、バージョン制約の指定をサポートしていません。

注意

このアプローチに対応するには、プロジェクトのビルド フラグを調整する必要がある場合があります。プロジェクトの CMax の呼び出しには、ビルドの一部としてC++ドライバーに通常渡されるフラグまたはオプションを含める必要があります。

add_subdirectory コマンドを使用するには、プロジェクトディレクトリに CMakeLists.txtファイルを作成します。次の例では、add_subdirectoryを使用する /home/user/project2プロジェクトディレクトリに CMakeLists.txtファイルを作成します。

/host/user/プロジェクト2/CMaxLists.txt
cmake_minimum_required (VERSION 3.15)
project (builder_basic LANGUAGES CXX)
# specify a source_dir which is out-of-tree from this project, so also specify bin_dir
add_subdirectory (/<path-to-mongo-cxx-driver-sources> ./build/mongo-cxx-driver)
add_executable (view_and_value /<path-to-mongo-cxx-driver-sources>/examples/bsoncxx/view_and_value.cpp)
# we need target_include_directories because view_and_value.cpp refers to a common example header
target_include_directories (view_and_value PRIVATE /<path-to-mongo-cxx-driver-sources>)
# no mongo:: namespace prefix, since the targets are use directly without installation
target_link_libraries (view_and_value PRIVATE bsoncxx_shared)

注意

上記の例では、mongo::名前空間のない bsoncxx_shared CSpec ターゲットを使用しています。名前空間は CMax モジュールのインストールの一部として追加されますが、このアプローチでは実行されません。

次に、以下のコマンドを実行してプロジェクトを構築します。

$ cd /home/user/project1
$ cmake -Bbuild
$ cmake --build build

これらのコマンドは、ビルドプロセスに関する情報を返します。ビルドが完了したら、前の手順で生成された実行可能ファイルを実行します。

$ ./build/view_and_value

出力は次のようになります。

{ "team" : "platforms", "id" : { "$oid" : "67207dcf532837a4470cc090" }, "members" : [ "tyler", "jason", "drew", "sam", "ernie", "john", "mark", "crystal" ] }
Got key, key = team
Got String!
Got key, key = id
Got ObjectId!
Got key, key = members
Got Array!
array element: tyler
array element: jason
array element: drew
array element: sam
array element: ernie
array element: john
array element: mark
array element: crystal
as expected, we have a team
document has 3 keys.
document keys are:
team
id
members

プロジェクトが Cake ベースでない場合は、pkg-config を使用してC++ドライバーをプロジェクトと統合できます。 pkg-config は CSpec よりも柔軟性が低いため、可能な場合は CSpec ベースのアプローチを使用することをお勧めします。

ドライバーを完全にインストールする場合にのみ、 pkg-config でC++ドライバーを使用できます。

次のコードでは、pkg-config を使用してC++ドライバーを含め、リンクします。以下のように、<path-to-mongo-cxx-driver-sources> プレースホルダーをシステム上のC++ドライバー ソース ツリーのロケーションに置き換えます。

$ c++ /<path-to-mongo-cxx-driver-sources>/examples/bsoncxx/view_and_value.cpp $(pkg-config --cflags libbsoncxx) -I/<path-to-mongo-cxx-driver-sources> $(pkg-config --libs libbsoncxx) -o view_and_value
$ ./view_and_value
{ "team" : "platforms", "id" : { "$oid" : "67207262672b96dc3b0fc150" }, "members" : [ "tyler", "jason", "drew", "sam", "ernie", "john", "mark", "crystal" ] }
Got key, key = team
Got String!
Got key, key = id
Got ObjectId!
Got key, key = members
Got Array!
array element: tyler
array element: jason
array element: drew
array element: sam
array element: ernie
array element: john
array element: mark
array element: crystal
as expected, we have a team
document has 3 keys.
document keys are:
team
id
members

上記のコマンドラインは、より複雑なプロジェクトや特定のビルドシステム用に、 pkg-config パッケージの消費方法に応じて調整できます。

戻る

高度な構成とインストール

項目一覧