高级配置和安装选项
本页内容
集成商的附加选项
如果您正在构建BSON C++库和/或C++驱动程序以嵌入其他组件,并且希望避免与标准构建或从发行版软件包管理器安装的组件发生冲突,您可以使用BSONCXX_OUTPUT_BASENAME
和MONGOCXX_OUTPUT_BASENAME
选项来配置cmake
。
cmake .. \ -DBSONCXX_OUTPUT_BASENAME=custom_bsoncxx \ -DMONGOCXX_OUTPUT_BASENAME=custom_mongocxx
上述命令将生成名为libcustom_bsoncxx.so
和libcustom_mongocxx.so
(或根据构建平台适当的扩展名)的库(或)。这些库可以放置在标准系统目录或备用位置,并且可以通过在链接器命令行上指定类似于-lcustom_mongocxx -lcustom_bsoncxx
的方式来链接(可能需要调整特定于您的链接器的特定标志)。
使用软件包管理器安装
您可以使用以下软件包管理器安装 C++ 驱动程序
Vcpkg 安装说明
如果您尚未安装 Vcpkg,请使用以下命令进行安装
$ git clone https://github.com/Microsoft/vcpkg.git $ cd vcpkg $ ./bootstrap-vcpkg.sh
可选:使用 Visual Studio 集成安装
vcpkg integrate install
安装驱动程序。您可能需要 git pull
以获取驱动程序的最新版本。
$ ./vcpkg install mongo-cxx-driver
您可以使用工具链文件 vcpkg.cmake
来指示 CMake 在哪里查找开发文件,例如
-DCMAKE_TOOLCHAIN_FILE=/<path to vcpkg repo>/vcpkg/scripts/buildsystems/vcpkg.cmake
头文件位于
vcpkg/installed/<CPU ARCHITECTURE>-<OPERATING SYSTEM>/include/
库文件位于
vcpkg/installed/<CPU ARCHITECTURE>-<OPERATING SYSTEM>/lib/
Conan 安装说明
包指定器: mongo-cxx-driver/3.8.0
如果您尚未安装 Conan,请先安装它并运行以下 Conan 初始化命令
$ pip install conan $ conan profile detect --force
将以下内容添加到您的 conanfile.txt
[requires] mongo-cxx-driver/3.8.0 [generators] CMakeDeps CMakeToolchain
通过 Conan 安装驱动程序,并构建您的项目
$ conan install conanfile.txt --output-folder=build --build=missing $ cmake \ -B build \ -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake \ -DCMAKE_BUILD_TYPE=Release $ cmake --build build
Homebrew 安装说明
MacOS 用户可以通过运行以下命令来使用 Homebrew 安装 C++ 驱动程序
brew install mongo-cxx-driver
对于 Apple Silicon Mac
头文件可以在以下位置找到
/opt/homebrew/include/mongocxx/v_noabi/ /opt/homebrew/include/bsoncxx/v_noabi/
库文件可以在以下位置找到
/opt/homebrew/lib/
对于 Intel Mac
头文件可以在以下位置找到
/usr/local/include/mongocxx/v_noabi/ /usr/local/include/bsoncxx/v_noabi/
库文件可以在以下位置找到
/usr/local/lib/
安装 MongoDB C 驱动程序
mongocxx 驱动程序基于 MongoDB C 驱动程序。
mongocxx-3.9.0 的构建会自动检测并下载安装 C 驱动,如果未检测到 C 驱动。要使用现有的 C 驱动安装,请将 CMAKE_PREFIX_PATH
设置为包含 C 驱动安装的目录。关于每个 C++ 驱动版本所需的最低 libmongoc
版本信息,请参阅libmongoc 兼容性.
除非你知道你的软件包管理器提供了足够新的版本,否则你需要从源代码下载和构建。从C 驱动发布 页面下载 tar 包。
请按照安装 libmongoc 的说明从 tar 包构建。
行业最佳实践和某些法规要求使用 TLS 1.1 或更高版本。MongoDB C 驱动在 Linux 上支持 TLS 1.1(如果 OpenSSL 至少是 1.0.1 版本)。在 macOS 和 Windows 上,C 驱动使用支持 TLS 1.1 的本地 TLS 实现。
高级配置(静态配置)
以下子部分详细说明了配置C++驱动及其依赖项为静态库而不是典型共享库的高级选项。这些选项将生成行为不同的库工件。在利用这些选项之前,请确保您完全理解各种链接方法的影响。
使用mongocxx
3.2.x或更高版本
用户可以选择将mongocxx
构建为静态库。不建议初学者使用。用户可以通过-DBUILD_SHARED_LIBS
选项启用此行为
cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF
使用mongocxx
3.5.0或更高版本
用户可以选择将mongocxx
构建为静态库和共享库。用户可以通过-DBUILD_SHARED_AND_STATIC_LIBS
选项启用此行为
cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_AND_STATIC_LIBS=ON
用户可以选择将 mongocxx
构建为一个静态链接的共享库,其中包含 libmongoc
。 不建议新手用户这样做。 用户可以通过使用 -DBUILD_SHARED_LIBS_WITH_STATIC_MONGOC
选项来启用此行为
cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS_WITH_STATIC_MONGOC=ON
禁用测试
通过将 -DENABLE_TESTS=OFF
作为 cmake 选项传递,可以禁用测试目标的配置。
cmake .. -DENABLE_TESTS=OFF cmake --build .. --target help # No test targets are configured.
安装到非标准目录
要将 C++ 驱动程序安装到非标准目录,请将 CMAKE_INSTALL_PREFIX
设置为所需的安装路径
cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=$HOME/mongo-cxx-driver
还可以指定 -DCMAKE_INSTALL_RPATH=
选项到安装的 lib
目录。这可能会使 libmongocxx.so 能够定位到 libbsoncxx.so
cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=$HOME/mongo-cxx-driver \ -DCMAKE_INSTALL_RPATH=$HOME/mongo-cxx-driver/lib
如果 C 驱动程序安装到非标准目录,请将 CMAKE_PREFIX_PATH
设置为 C 驱动程序的安装路径
cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_PREFIX_PATH=$HOME/mongo-c-driver \ -DCMAKE_INSTALL_PREFIX=$HOME/mongo-cxx-driver
注意
如果您需要在 CMake PATH 变量中包含多个路径,请使用分号进行分隔,如下所示: -DCMAKE_PREFIX_PATH="/your/cdriver/prefix;/some/other/path"
使用 mongocxx 3.1.x 或 3.0.x 配置
用户必须通过使用 -DLIBMONGOC_DIR
和 -DLIBBSON_DIR
选项来指定 libmongoc
安装目录,而不是使用 -DCMAKE_PREFIX_PATH
选项
cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DLIBMONGOC_DIR=$HOME/mongo-c-driver \ -DLIBBSON_DIR=$HOME/mongo-c-driver \ -DCMAKE_INSTALL_PREFIX=$HOME/mongo-cxx-driver
故障排除
在 macOS 上修复 库未加载
错误
链接到非标准目录安装的应用程序在运行时加载 C++ 驱动程序可能会遇到错误。示例
# Tell pkg-config where to find C++ driver installation. export PKG_CONFIG_PATH=$HOME/mongo-cxx-driver/lib/pkgconfig clang++ app.cpp -std=c++11 $(pkg-config --cflags --libs libmongocxx) -o ./app.out ./app.out # Prints the following error: # dyld[3217]: Library not loaded: '@rpath/libmongocxx._noabi.dylib' # Referenced from: '/Users/kevin.albertson/code/app.out' # Reason: tried: '/usr/local/lib/libmongocxx._noabi.dylib' (no such file), '/usr/lib/libmongocxx._noabi.dylib' (no such file) # zsh: abort ./app.out
macOS 上 C++ 驱动程序的默认 安装名称
包含 @rpath
otool -D $HOME/mongo-cxx-driver/lib/libmongocxx.dylib # Prints: # /Users/kevin.albertson/mongo-cxx-driver/lib/libmongocxx.dylib: # @rpath/libmongocxx._noabi.dylib
在安装名称中包含 @rpath
允许应用程序控制库的搜索路径列表。
app.out
包含加载命令 @rpath/libmongocxx._noabi.dylib
。 app.out
没有替换 @rpath
的条目。
在 macOS 上有几种方法可以考虑解决这个问题
将 DYLD_FALLBACK_LIBRARY_PATH
传递到包含 C++ 驱动程序库的目录
DYLD_FALLBACK_LIBRARY_PATH=$HOME/mongo-cxx-driver/lib ./app.out # Prints "successfully connected with C++ driver"
或者,可以将链接器选项 -Wl,-rpath
传递以添加替换 @rpath
的条目
# Tell pkg-config where to find C++ driver installation. export PKG_CONFIG_PATH=$HOME/mongo-cxx-driver/lib/pkgconfig # Pass the linker option -rpath to set an rpath in the final executable. clang++ app.cpp -std=c++11 -Wl,-rpath,$HOME/mongo-cxx-driver/lib $(pkg-config --cflags --libs libmongocxx) -o ./app.out ./app.out # Prints "successfully connected with C++ driver"
如果使用cmake构建应用程序,则构建树中所有使用的库将包含完整的RPATH。然而,在安装时,cmake将清除这些目标的RPATH,因此它们将以空RPATH安装。这可能会导致安装后出现 库未加载
错误。
示例
# Build application ``app`` using the C++ driver from a non-standard install. cmake \ -DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \ -DCMAKE_INSTALL_PREFIX=$HOME/app \ -DCMAKE_CXX_STANDARD=11 \ -Bcmake-build -S. cmake --build cmake-build --target app.out # Running app.out from build tree includes rpath to C++ driver. ./cmake-build ./cmake-build/app.out # Prints: "successfully connected with C++ driver" cmake --build cmake-build --target install # Running app.out from install tree does not include rpath to C++ driver. $HOME/app/bin/app.out # Prints "Library not loaded" error.
考虑设置 -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE
,以便将可执行文件的rpath保留在安装目标中。
# Build application ``app`` using the C++ driver from a non-standard install. # Use CMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE to keep rpath entry on installed app. cmake \ -DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \ -DCMAKE_INSTALL_PREFIX=$HOME/app \ -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE \ -DCMAKE_CXX_STANDARD=11 \ -Bcmake-build -S. cmake --build cmake-build --target install $HOME/app/bin/app.out # Prints "successfully connected with C++ driver"
有关更多信息,请参阅cmake文档中的RPATH处理。
在Linux上修复“无法打开共享对象文件”错误
链接到非标准目录安装的应用程序在运行时加载 C++ 驱动程序可能会遇到错误。示例
# Tell pkg-config where to find C++ driver installation. export PKG_CONFIG_PATH=$HOME/mongo-cxx-driver/lib/pkgconfig g++ -std=c++11 app.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out ./app.out # Prints the following error: # ./app.out: error while loading shared libraries: libmongocxx.so._noabi: cannot open shared object file: No such file or directory
在Linux上有几种方法可以考虑解决这个问题
将 LD_LIBRARY_PATH
传递到包含C++驱动库的目录
LD_LIBRARY_PATH=$HOME/mongo-cxx-driver/lib ./app.out # Prints "successfully connected with C++ driver"
或者,可以将链接器选项 -Wl,-rpath
传递以添加 rpath
条目
# Tell pkg-config where to find C++ driver installation. export PKG_CONFIG_PATH=$HOME/mongo-cxx-driver/lib/pkgconfig # Pass the linker option -rpath to set an rpath in the final executable. g++ app.cpp -std=c++11 -Wl,-rpath,$HOME/mongo-cxx-driver/lib $(pkg-config --cflags --libs libmongocxx) -o ./app.out ./app.out # Prints "successfully connected with C++ driver"
如果使用cmake构建应用程序,则构建树中所有使用的库将包含完整的RPATH。然而,在安装时,cmake将清除这些目标的RPATH,因此它们将以空RPATH安装。这可能会导致安装后出现 库未加载
错误。
示例
# Build application ``app`` using the C++ driver from a non-standard install. cmake \ -DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \ -DCMAKE_INSTALL_PREFIX=$HOME/app \ -DCMAKE_CXX_STANDARD=11 \ -Bcmake-build -S. cmake --build cmake-build --target app.out # Running app.out from build tree includes rpath to C++ driver. ./cmake-build ./cmake-build/app.out # Prints: "successfully connected with C++ driver" cmake --build cmake-build --target install # Running app.out from install tree does not include rpath to C++ driver. $HOME/app/bin/app.out # Prints "cannot open shared object file" error.
考虑设置 -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE
,以便将可执行文件的rpath保留在安装目标中。
# Build application ``app`` using the C++ driver from a non-standard install. # Use CMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE to keep rpath entry on installed app. cmake \ -DCMAKE_PREFIX_PATH=$HOME/mongo-cxx-driver \ -DCMAKE_INSTALL_PREFIX=$HOME/app \ -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE \ -DCMAKE_CXX_STANDARD=11 \ -Bcmake-build -S. cmake --build cmake-build --target install $HOME/app/bin/app.out # Prints "successfully connected with C++ driver"
有关更多信息,请参阅cmake文档中的RPATH处理。