本文为在Deepin20.9环境下安装mongoc++驱动的过程记录。
操作系统:Deepin 20.9
mongo版本:docker mongoDB 5.0.17
gcc版本:12.2.0 默认支持C++17 要求C++11及以上
cmake版本:3.24.2 要求3.2及以上
openssl版本:1.1.1d17 要求1.1及以上
查看下表支持mongoDB5.0的mongocxx-driver驱动版本为3.7
Mongoc++驱动程序建立在 MongoDB C 驱动之上,mongocxx-3.7.x需要libmongoc 1.22.1 或更高版本。
(1)检查包管理器提供的libmongoc版本
$ apt list libmongoc*
正在列表... 完成
libmongoc-1.0-0/未知,now 1.14.0-1 amd64 [已安装]
libmongoc-1.0-0/未知 1.14.0-1 i386
libmongoc-dev/未知,now 1.14.0-1 amd64 [已安装]
libmongoc-dev/未知 1.14.0-1 i386
libmongoc-doc/未知,未知,now 1.14.0-1 all [已安装]
libmongoclient-dev/未知 1.1.3-3 amd64
libmongoclient-dev/未知 1.1.3-3 i386
libmongoclient0/未知 1.1.3-3 amd64
libmongoclient0/未知 1.1.3-3 i386
$
(2)如果包管理器提供的libmongoc版本>1.22.1,则使用包管理器安装:
$ sudo apt install libmongoc-1.* libbson-1.*
(3)此处包管理器提供的libmongoc版本为1.14.0-1<1.22.1,则需要下载最新的源代码构建。
(1)准备源代码
①查看最新源代码版本
查看URL地址:
https://github.com/mongodb/mongo-c-driver/releases
经查mongoc当前最新版本为1.23.4。
②下载mongo-c-driver源代码
$ wget https://github.com/mongodb/mongo-c-driver/releases/download/ 1.23.4/mongo-c-driver-1.23.4.tar.gz
............
已发出 HTTP 请求,正在等待回应... 200 OK
长度:7795336 (7.4M) [application/octet-stream]
正在保存至: “mongo-c-driver-1.23.4.tar.gz”
mongo-c-driver-1.23.4.tar.gz 100%[=======================================>] 7.43M 19.6KB/s 用时 6m 29s =
2023-05-11 11:02:46 (19.6 KB/s) - 已保存 “mongo-c-driver-1.23.4.tar.gz” [7795336/7795336])
$
③解压源代码包
$ tar xzf mongo-c-driver-1.23.4.tar.gz
(2)准备构建环境
$ sudo sh -c 'curl -s --location https://www.mongodb.org/static/pgp/libmongocrypt.asc | gpg --dearmor >/etc/apt/trusted.gpg.d/libmongocrypt.gpg'
echo "deb https://libmongocrypt.s3.amazonaws.com/apt/debian buster/libmongocrypt/1.7 main" | sudo tee /etc/apt/sources.list.d/libmongocrypt.list
$ sudo apt update
$ sudo apt install lld glibc-source gawk bison libzstd-dev libssl-dev libsasl2-dev libmongocrypt-dev
(3)构建安装libmongoc
默认安装到目录/usr/local下,即包在/usr/local/lib下,头文件在/usr/local/include下。
$ cd mongo-c-driver-1.23.4
$ mkdir cmake-build
$ cd cmake-build
$ cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..
......
$ make
......
$ sudo make install
......
Install the project...
-- Install configuration: "RelWithDebInfo"
......
-- Installing: /usr/local/lib/libbson-1.0.so.0.0.0
-- Up-to-date: /usr/local/lib/libbson-1.0.so.0
-- Up-to-date: /usr/local/lib/libbson-1.0.so
-- Installing: /usr/local/lib/libbson-static-1.0.a
......
-- Installing: /usr/local/lib/libmongoc-1.0.so.0.0.0
-- Up-to-date: /usr/local/lib/libmongoc-1.0.so.0
-- Set runtime path of "/usr/local/lib/libmongoc-1.0.so.0.0.0" to ""
-- Up-to-date: /usr/local/lib/libmongoc-1.0.so
-- Installing: /usr/local/lib/libmongoc-static-1.0.a
$
待查问题:
Policy CMP0075 is not set: Include file check macros honor -- Looking for strlcpy - not found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for res_ndestroy - not found
-- Looking for sched_getcpu - not found
1、下载mongoc++源代码
①查看最新mongoc++驱动版本
查看URL地址:
https://github.com/mongodb/mongo-cxx-driver/releases
最新mongoc++驱动版本为3.7.1:
②下载mongoc++驱动
$ curl -OL https://github.com/mongodb/mongo-cxx-driver/releases/download/r3.7.1/mongo-cxx-driver-r3.7.1.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
100 1411k 100 1411k 0 0 58876 0 0:00:24 0:00:24 --:--:-- 36445
$
③解压源代码包
$ tar -xzf mongo-cxx-driver-r3.7.1.tar.gz
$ cd mongo-cxx-driver-r3.7.1/build
2、配置构建
(1)构建选项说明
指定构建类型
-DCMAKE_BUILD_TYPE={[RelWithDebInfo]|Release|Debug}
RelWithDebInfo 默认构建带调试的发布包
Release 构建发布包
Debug 构建调试包
指定依赖的mongoc驱动库路径,当有多个依赖库路径时,用分号分隔并用双引号包括。
-DCMAKE_PREFIX_PATH=<依赖mongo-c库路径>
指定mongocxx驱动库的安装路径
-DCMAKE_INSTALL_PREFIX=
指定mongocxx驱动库的非标准安装路径
-DCMAKE_INSTALL_RPATH=
指定同时构建动态和静态库,默认只构建动态库
-DBUILD_SHARED_AND_STATIC_LIBS=ON
例:安装到标准路径/usr/local
cmake .. -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local
例:安装到非标准路径/opt/mongo
cmake .. -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/opt/mongo \
-DCMAKE_INSTALL_RPATH=/opt/mongo
例:构建时指定libmongoc路径/opt/mongo-c-driver和mongocxx安装路径/opt/mongo-cxx-driver
cmake .. -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=/opt/mongo-c-driver \
-DCMAKE_INSTALL_PREFIX=/opt/mongo-cxx-driver
(2)配置
本文使用的构建配置:
.../build$ cmake .. -DCMAKE_BUILD_TYPE=Release \
-DBSONCXX_POLY_USE_MNMLSTC=1 \
-DCMAKE_INSTALL_PREFIX=/usr/local
......
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
......
-- Build files have been written to: /home/xman/mongodb-driver/mongo-cxx-driver-r3.7.1/build
.../build$
3、构建安装
(1)安装MNMLSTC
.../build$ sudo cmake --build . --target EP_mnmlstc_core
......
[100%] Built target EP_mnmlstc_core
.../build$
(2)构建
.../build$ cmake --build .
[ 1%] Built target EP_mnmlstc_core
......
[ 4%] Linking CXX shared library libbsoncxx.so
[ 4%] Built target bsoncxx_shared
......
[ 35%] Linking CXX shared library libmongocxx.so
[ 35%] Built target mongocxx_shared
......
[100%] Built target test_versioned_api
.../build$
(3)安装
.../build$ sudo cmake --build . --target install
[ 1%] Built target EP_mnmlstc_core
Consolidate compiler generated dependencies of target bsoncxx_shared
[ 4%] Built target bsoncxx_shared
......
[ 35%] Built target mongocxx_shared
......
[100%] Built target test_versioned_api
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/share/mongo-cxx-driver/LICENSE
......
-- Installing: /usr/local/lib/libbsoncxx.so.3.7.1
-- Installing: /usr/local/lib/libbsoncxx.so._noabi
-- Set runtime path of "/usr/local/lib/libbsoncxx.so.3.7.1" to "/usr/local/lib"
-- Installing: /usr/local/lib/libbsoncxx.so
......
-- Installing: /usr/local/lib/pkgconfig/libbsoncxx.pc
-- Installing: /usr/local/include/mongocxx/v_noabi/mongocxx
......
-- Installing: /usr/local/lib/libmongocxx.so.3.7.1
-- Installing: /usr/local/lib/libmongocxx.so._noabi
-- Set runtime path of "/usr/local/lib/libmongocxx.so.3.7.1" to "/usr/local/lib"
-- Installing: /usr/local/lib/libmongocxx.so
......
-- Installing: /usr/local/share/mongo-cxx-driver/uninstall.sh
.../build$
(4)卸载
卸载方式一:
$ sudo cmake --build . --target uninstall
卸载方式二:
$ sudo /usr/local/share/mongo-cxx-driver/uninstall.sh