Overview
在本指南中,您可以学习如何使用 CMake 和 pkg-config 将 C++ 驱动程序包含在项目中。
样本数据
本页示例使用了 view_and_value.cpp 示例程序,该程序来自 C++ 驱动程序源代码。通过遵循下载并安装指南获取 C++ 驱动程序源代码。
在以下各节中,将 <path-to-mongo-cxx-driver-sources> 替换为C++驱动程序源代码树在系统上的实际路径。
您不需要在MongoDB服务器上运行此程序。
库引用
本页上的示例引用了C++驱动程序库目标。CMake 部分中的示例使用 mongo::bsoncxx_shared 目标,pkg-config 部分中的示例使用 libbsoncxx包。您可以根据项目的需求,使用以下替代库目标:
CMake:-
mongo::bsoncxx_shared-mongo::mongoccxx_shared-mongo::bsoncxx_static-mongo::mongoccxx_staticpkg-config: -
libbsoncxx-libmongocxx-libbsoncxx-static-libmongocxx-static
目标的可用性取决于特定的安装方法。
CMake
您可以使用 CMake 将 C++ 驱动程序包含在项目中。CMake 提供了 find_package 命令,您的项目可以在安装 C++ 驱动程序后使用它来定位。或者,您的项目可以使用高级 add_subdirectory 命令,而无需安装C++驱动程序。
故障排除
在以下部分中,如果遇到错误,您可能需要为初始 CMake 命令指定其他选项。具体选项取决于您的特定环境。但是,以下选项解决最常见的问题:
如果您的编译器的默认编译版本至少为C++17,请使用
-DCMAKE_CXX_STANDARD=17CMake 选项。如果将驱动程序安装到非标准位置,请指定
-DCMAKE_PREFIX_PATH=/<path-to-mongo-cxx-driver-installation>选项。示例:
cmake -Bbuild -DCMAKE_CXX_STANDARD=17 -DCMAKE_PREFIX_PATH=/opt/mongodb/cxx-driver安装驱动程序程序
安装C++驱动程序后,您可以使用 CMake 的 find_package 命令将驱动程序集成到您的项目中。
要使用 find_package 命令,请在项目目录中创建 CMakeLists.txt文件。以下示例在 /home/user/project1项目目录中创建一个使用 find_package 的 CMakeLists.txt文件:
/home/user/ 项目1/CMakeLists.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 headertarget_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 = teamGot String!Got key, key = idGot ObjectId!Got key, key = membersGot Array!array element: tylerarray element: jasonarray element: drewarray element: samarray element: erniearray element: johnarray element: markarray element: crystalas expected, we have a teamdocument has 3 keys.document keys are:teamidmembers无需安装驱动程序程序
或者,您可以使用 CMake 的 add_subdirectory 命令,而无需安装 C++ 驱动程序。这是一项高级技术,与 find_package 命令不同,它不支持指定版本约束。
注意
您可能需要调整项目的构建标志以适应这种方法。项目对 CMake 的调用必须包含通常作为其构建的一部分传递给 C++ 驱动程序的任何标志或选项。
要使用 add_subdirectory 命令,请在项目目录中创建 CMakeLists.txt文件。以下示例在 /home/user/project2项目目录中创建一个使用 add_subdirectory 的 CMakeLists.txt文件:
/home/user/ 项目2/CMakeLists.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_diradd_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 headertarget_include_directories (view_and_value PRIVATE /<path-to-mongo-cxx-driver-sources>)# no mongo:: namespace prefix, since the targets are use directly without installationtarget_link_libraries (view_and_value PRIVATE bsoncxx_shared)注意
前面的示例使用了不带 mongo::命名空间的 bsoncxx_shared CMake 目标。命名空间作为 CMake 模块安装的一部分添加,但在此方法中不会执行此操作。
然后,运行以下命令来构建项目:
$ 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 = teamGot String!Got key, key = idGot ObjectId!Got key, key = membersGot Array!array element: tylerarray element: jasonarray element: drewarray element: samarray element: erniearray element: johnarray element: markarray element: crystalas expected, we have a teamdocument has 3 keys.document keys are:teamidmemberspkg-config
如果您的项目不是基于 CMake 的,则可以使用 pkg-config 将 C++ 驱动程序与项目集成。由于 pkg-config 提供的灵活性低于 CMake,因此我们建议尽可能使用基于 CMake 的方法。
如果完全安装了驱动程序,则只能将C++驱动程序与 pkg-config 一起使用。
以下代码使用 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 = teamGot String!Got key, key = idGot ObjectId!Got key, key = membersGot Array!array element: tylerarray element: jasonarray element: drewarray element: samarray element: erniearray element: johnarray element: markarray element: crystalas expected, we have a teamdocument has 3 keys.document keys are:teamidmembers您可以根据更复杂的项目或特定的构建系统调整前面的命令行,具体取决于它们使用 pkg-config 包的方式。