在程序中包含并链接驱动程序
Overview
在本指南中,您可以学习;了解如何使用 CMake 和 pkg-config
将C++驾驶员包含在项目中。
样本数据
本页上的示例使用C++驾驶员源代码中的 view_and_value.cpp 示例程序。按照 下载和安装指南获取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_static
pkg-config: -
libbsoncxx
-libmongocxx
-libbsoncxx-static
-libmongocxx-static
目标的可用性取决于特定的安装方法。
CMake
您可以使用 CMake 将C++驾驶员包含在项目中。 CMake 提供了 find_package
命令,您的项目可以在安装C++驾驶员后使用该命令来查找该驱动程序。或者,您的项目可以使用高级 add_subdirectory
命令,而无需安装C++驾驶员。
故障排除
在以下部分中,如果遇到错误,您可能需要为初始 CMake 命令指定其他选项。具体选项取决于您的特定环境。但是,以下选项解决最常见的问题:
如果您的编译器的默认编译版本至少为C++17,请使用
-DCMAKE_CXX_STANDARD=17
CMake 选项。如果将驾驶员安装到非标准位置,请指定
-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
命令将驾驶员集成到您的项目中。
提示
要学习;了解如何安装C++驾驶员,请访问以下指南:
要使用 find_package
命令,请在项目目录中创建 CMakeLists.txt
文件。以下示例在 /home/user/project1
项目目录中创建一个使用 find_package
的 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 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
无需安装驱动程序程序
或者,您可以使用 CMake 的 add_subdirectory
命令,而无需安装C++驾驶员。这是一项高级技术,与 find_package
命令不同,它不支持指定版本约束。
注意
您可能需要调整项目的构建标志以适应这种方法。项目对 CMake 的调用必须包含通常作为其构建的一部分传递给C++驾驶员的任何标志或选项。
要使用 add_subdirectory
命令,请在项目目录中创建 CMakeLists.txt
文件。以下示例在 /home/user/project2
项目目录中创建一个使用 add_subdirectory
的 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_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
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 = 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
如果您的项目不是基于 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 = 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 包的方式。