VS2017 + Windows10编译C++ MongoDB驱动
<p>转载地址:https://blog.csdn.net/sqcfj/article/details/86716831</p><p>mongoDB有两个接口库:mongo-c-driver和mongo-cxx-driver</p>
<p>前者是c代码接口库,后者是c++代码接口库,是在mongo-c-driver的基础上进行二次封装</p>
<h1>一、准备工作</h1>
<p>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)</p>
<p>2、下载mongo-cxx-driver源码:https://github.com/mongodb/mongo-cxx-driver/releases(3.4.0版本)</p>
<p>3、下载boost:https://www.boost.org/users/history/version_1_70_0.html(此为下载源码,需要自己编译 )</p>
<p>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安装包 )</p>
<h1>二、boost编译</h1>
<p>下面为编译boost步骤:(这个其实没有必要,可以跳过,全当体验编译过程。。。)</p>
<p>在根目录下运行bootstrap.bat</p>
<p><img src="https://img2018.cnblogs.com/blog/1604637/201907/1604637-20190709230948258-2075608006.png" alt="" width="804" height="420"></p>
<p>生成了这两个东西</p>
<p><img src="https://img2018.cnblogs.com/blog/1604637/201907/1604637-20190709231123455-892152998.png" alt="" width="801" height="428"></p>
<p>然后修改这个配置文件</p>
<p><img src="https://img2018.cnblogs.com/blog/1604637/201907/1604637-20190709231510248-1597764084.png" alt="" width="797" height="426"></p>
<p>改成这样(话说也不知道有什么用)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># Boost.Build Configuration
# Automatically generated by bootstrap.bat
import option ;
</span><span style="color: rgba(0, 0, 255, 1)">using</span> msvc : <span style="color: rgba(128, 0, 128, 1)">14.0</span> : <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\bin\Hostx64\x64\cl.exe</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
option.</span><span style="color: rgba(0, 0, 255, 1)">set</span> keep-going : <span style="color: rgba(0, 0, 255, 1)">false</span> ;</pre>
</div>
<p>然后在开始菜单VS2017工具包下运行命令(随便打开一个CMD会找不到xxx文件错误......):</p>
<div class="cnblogs_code">
<pre>b2 stage --toolset=msvc-<span style="color: rgba(128, 0, 128, 1)">14.0</span> address-model=<span style="color: rgba(128, 0, 128, 1)">64</span> --stagedir=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">E:\Library\boost_1_70_0\bin\VC14</span><span style="color: rgba(128, 0, 0, 1)">"</span> threading=multi --build-type=complete</pre>
</div>
<p>参数按照博主的说明设置</p>
<p><img src="https://img2018.cnblogs.com/blog/1604637/201907/1604637-20190710000619968-1810852350.png" alt="" width="811" height="424"></p>
<p>这个编译需要一些时间,可以睡一觉......</p>
<p> 编译完成......内容和.exe安装解压后的是一样的,就是版本更新一点</p>
<p><img src="https://img2018.cnblogs.com/blog/1604637/201907/1604637-20190710074026731-619268641.png" alt="" width="805" height="421"></p>
<h1> 三、mongo-c-driver-1.13.0编译</h1>
<p>由于源码中已经有了build文件夹,为了不和其中的混合,新建cmake-build,在此目录下打开cmd或power shell,指向以下命令(文件目录替换成自己的)</p>
<div class="cnblogs_code">
<pre>cmake -G <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Visual Studio 15 2017 Win64</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-DCMAKE_INSTALL_PREFIX=E:\Library\mongo-c-driver-1.13.0</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-DCMAKE_PREFIX_PATH=E:\Library\mongo-c-driver-1.13.0</span><span style="color: rgba(128, 0, 0, 1)">"</span> ..</pre>
</div>
<p>然后生成就会生成解决方案,打开.sln文件,选择32或64位,分别编译生成ALL_BUILD, INSTALL两个项目</p>
<p><img src="https://img2018.cnblogs.com/blog/1604637/201907/1604637-20190710224233737-1367412259.png" alt="" width="813" height="434"></p>
<p><img src="https://img2018.cnblogs.com/blog/1604637/201907/1604637-20190710224341700-1472510196.png" alt="" width="604" height="657"><img src="https://img2018.cnblogs.com/blog/1604637/201907/1604637-20190710224433780-1795178331.png" alt="" width="606" height="655"></p>
<p>至此第一个c驱动编译完成,在根目录下已生成bin, build, lib文件夹</p>
<h1>四、mongo-cxx-driver-r3.4.0编译</h1>
<p>编译第二个c++驱动,此时需要用到boost库(这里使用的.exe安装的boost),步骤基本相同,源码中有空的build目录,就直接在这里编译了</p>
<div class="cnblogs_code">
<pre>cmake -G <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Visual Studio 15 2017 Win64</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-DCMAKE_INSTALL_PREFIX=E:\Library\mongo-cxx-driver-r3.4.0</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-DCMAKE_PREFIX_PATH=E:\Library\mongo-c-driver-1.13.0</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-DBOOST_ROOT=E:\Library\boost_1_67_0</span><span style="color: rgba(128, 0, 0, 1)">"</span> ..</pre>
</div>
<p>然后和步骤和上面一样,打开.sln解决方案,分别ALL_BUILD, INSTALL,等待生成三个文件夹</p>
<p> 第二个c++驱动编译编译完成</p>
<p>使用的时候,需要将mongo-cxx-driver生成的include目录和mongo-cxx-driver生成的include目录,以及boost库目录都添加进去,链接上述生成的lib文件,并且把生成的dll放到可执行文件一起就可以了。到此windows平台下C++版本的mongo driver编译完成。</p>
<p>只有自己编译boost的过程比较麻烦,但可以选择使用现成的编译好的安装包</p>
<p>然后注意c和c++驱动的版本对应问题,应该不会有什么错误</p>
<h1>五、重要说明</h1>
<p>我在编译完成了以上两个库之后,在配置使用c++驱动时报了bsoncxx/types.hpp头文件错误,在该文件中所有的该结构体type_id初始化失败</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">struct</span><span style="color: rgba(0, 0, 0, 1)"> BSONCXX_API b_utf8 {
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 255, 1)">static</span> constexpr auto <span style="color: rgba(255, 0, 0, 1)">type_id</span> =<span style="color: rgba(0, 0, 0, 1)"> type::k_utf8;
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(128, 128, 128, 1)">///</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Constructor for b_utf8.
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(128, 128, 128, 1)">///</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> @param value
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> The value to wrap.
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(128, 128, 128, 1)">///
</span><span style="color: rgba(0, 128, 128, 1)">10</span> template <<span style="color: rgba(0, 0, 0, 1)">typename T,
</span><span style="color: rgba(0, 128, 128, 1)">11</span> typename std::enable_if<!std::is_same<b_utf8, typename std::decay<T>::type><span style="color: rgba(0, 0, 0, 1)">::value,
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 255, 1)">int</span>>::type = <span style="color: rgba(128, 0, 128, 1)">0</span>>
<span style="color: rgba(0, 128, 128, 1)">13</span> BSONCXX_INLINE <span style="color: rgba(0, 0, 255, 1)">explicit</span> b_utf8(T&& t) : value(std::forward<T><span style="color: rgba(0, 0, 0, 1)">(t)) {}
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)"> stdx::string_view value;
</span><span style="color: rgba(0, 128, 128, 1)">16</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(128, 128, 128, 1)">///</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> Conversion operator unwrapping a string_view
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(128, 128, 128, 1)">///
</span><span style="color: rgba(0, 128, 128, 1)">20</span> BSONCXX_INLINE <span style="color: rgba(0, 0, 255, 1)">operator</span> stdx::string_view() <span style="color: rgba(0, 0, 255, 1)">const</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> value;
</span><span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">23</span> };</pre>
</div>
<p>最后不得不放弃使用c++驱动,而使用c驱动,具体demo可以参考c驱动API,倒是更为简洁一点。具体操作见 MongoDB C Driver Tutorial。</p><br><br>
来源:https://www.cnblogs.com/jixiaohua/p/11161429.html
頁:
[1]