VS2017 + Windows10编译C++ MongoDB驱动

转载地址:https://blog.csdn.net/sqcfj/article/details/86716831

mongoDB有两个接口库:mongo-c-driver和mongo-cxx-driver

前者是c代码接口库,后者是c++代码接口库,是在mongo-c-driver的基础上进行二次封装

一、准备工作

1、下载mongo-c-driver源码:https://github.com/mongodb/mongo-c-driver/releases/tag/1.13.0(注意:3.4.0版本mongo-cxx-driver只能配1.13.0mongo-c-driver)

2、下载mongo-cxx-driver源码:https://github.com/mongodb/mongo-cxx-driver/releases(3.4.0版本)

3、下载boost:https://www.boost.org/users/history/version_1_70_0.html(此为下载源码,需要自己编译 )

4、安装boost:https://blog.csdn.net/s_lisheng/article/details/72871218(建议直接https://sourceforge.net/projects/boost/files/boost-binaries/1.68.0/下载安装编译好的boost_1_68_0-msvc-14.0-64.exe安装包 )

二、boost编译

下面为编译boost步骤:(这个其实没有必要,可以跳过,全当体验编译过程。。。)

在根目录下运行bootstrap.bat

生成了这两个东西

然后修改这个配置文件

改成这样(话说也不知道有什么用)

# Boost.Build Configuration 
# Automatically generated by bootstrap.bat 
 
import option ; 
 
using msvc : 14.0 : "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\bin\Hostx64\x64\cl.exe"; 
 
option.set keep-going : false ;

然后在开始菜单VS2017工具包下运行命令(随便打开一个CMD会找不到xxx文件错误......):

b2 stage --toolset=msvc-14.0 address-model=64 --stagedir="E:\Library\boost_1_70_0\bin\VC14" threading=multi --build-type=complete

参数按照博主的说明设置

这个编译需要一些时间,可以睡一觉......

 编译完成......内容和.exe安装解压后的是一样的,就是版本更新一点

 三、mongo-c-driver-1.13.0编译

由于源码中已经有了build文件夹,为了不和其中的混合,新建cmake-build,在此目录下打开cmd或power shell,指向以下命令(文件目录替换成自己的)

cmake -G "Visual Studio 15 2017 Win64" "-DCMAKE_INSTALL_PREFIX=E:\Library\mongo-c-driver-1.13.0" "-DCMAKE_PREFIX_PATH=E:\Library\mongo-c-driver-1.13.0" ..

然后生成就会生成解决方案,打开.sln文件,选择32或64位,分别编译生成ALL_BUILD, INSTALL两个项目

至此第一个c驱动编译完成,在根目录下已生成bin, build, lib文件夹

四、mongo-cxx-driver-r3.4.0编译

编译第二个c++驱动,此时需要用到boost库(这里使用的.exe安装的boost),步骤基本相同,源码中有空的build目录,就直接在这里编译了

cmake -G "Visual Studio 15 2017 Win64" "-DCMAKE_INSTALL_PREFIX=E:\Library\mongo-cxx-driver-r3.4.0" "-DCMAKE_PREFIX_PATH=E:\Library\mongo-c-driver-1.13.0" "-DBOOST_ROOT=E:\Library\boost_1_67_0" ..

然后和步骤和上面一样,打开.sln解决方案,分别ALL_BUILD, INSTALL,等待生成三个文件夹

 第二个c++驱动编译编译完成

使用的时候,需要将mongo-cxx-driver生成的include目录和mongo-cxx-driver生成的include目录,以及boost库目录都添加进去,链接上述生成的lib文件,并且把生成的dll放到可执行文件一起就可以了。到此windows平台下C++版本的mongo driver编译完成。

只有自己编译boost的过程比较麻烦,但可以选择使用现成的编译好的安装包

然后注意c和c++驱动的版本对应问题,应该不会有什么错误

五、重要说明

我在编译完成了以上两个库之后,在配置使用c++驱动时报了bsoncxx/types.hpp头文件错误,在该文件中所有的该结构体type_id初始化失败

 1 struct BSONCXX_API b_utf8 {
 2     static constexpr auto type_id = type::k_utf8;
 3 
 4     ///
 5     /// Constructor for b_utf8.
 6     ///
 7     /// @param value
 8     ///   The value to wrap.
 9     ///
10     template <typename T,
11               typename std::enable_if<!std::is_same<b_utf8, typename std::decay<T>::type>::value,
12                                       int>::type = 0>
13     BSONCXX_INLINE explicit b_utf8(T&& t) : value(std::forward<T>(t)) {}
14 
15     stdx::string_view value;
16 
17     ///
18     /// Conversion operator unwrapping a string_view
19     ///
20     BSONCXX_INLINE operator stdx::string_view() const {
21         return value;
22     }
23 };

最后不得不放弃使用c++驱动,而使用c驱动,具体demo可以参考c驱动API,倒是更为简洁一点。具体操作见 MongoDB C Driver Tutorial

posted @ 2019-07-10 23:06  jixhua  阅读(2492)  评论(1编辑  收藏  举报