您的位置:首页 > 编程语言 > Go语言

Windows系统上安装生成Mongo-cxx-driver的lib和include

2016-09-21 10:25 435 查看
在执行以下步骤时,请确认已安装cmake和boost,熟悉git clone 和 git checkout 命令

安装生成libbson的库和包

从git上面clone最新的libbson,

https://github.com/mongodb/libbson

完成后,依次执行以下命令

cd libbson
cmake.exe -G "Visual Studio 14 2015 Win64" "-DCMAKE_INSTALL_PREFIX=C:\mongo\mongocdriver"


打开libbson.sln文件,然后选择Release ,x64版,然后分别对ALL_BUILD.vcproj和INSTALL.vcprioj进行生成,(我的电脑是64位的,要根据自身电脑配置进行选择)如下图







安装monogo-c-driver库和包

从git 上面clone最新的mongo-c-driver,

https://github.com/mongodb/mongo-c-driver

完成后依次执行以下命令

cd mongo-c-criver
cmake.exe -G "Visual Studio 14 2015 Win64" "-DCMAKE_INSTALL_PREFIX=C:\mongo\mongocdriver" "-DBSON_ROOT_DIR=C:\mongo\mongocdriver"


打开libmongoc.sln,然后选择Release ,x64版,然后分别对ALL_BUILD.vcproj和INSTALL.vcprioj进行生成,类似libbson.sln的操作

安装mongo-cxx-driver

从git上面clone最新的mongo-cxx-driver

https://github.com/mongodb/mongo-cxx-driver

完成后依次执行以下命令:

cd mongo-cxx-driver
git checkout r3.0.2
cd build
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=C:\mongo\cxx-driver-r3.0.2 -DLIBBSON_DIR=C:\mongo\mongocdriver -DLIBMONGOC_DIR=C:\mongo\mongocdriver -DBOOST_ROOT=D:\local\boost_1_60_0 -DBOOST_LIBRARYDIR=D:\local\boost_1_60_0\lib64-msvc-14.0 ..


打开MONGO_CXX_DRIVER.sln文件,然后选择Release,x64版,然后分别对ALL_BUILD.vcproj和INSTALL.vcprioj进行生成

完成以下步骤后,可以在C:\mongo\cxx-driver-r3.0.2看到相应的lib和include目录,此时在include文件夹是没有直接包含bsoncxx和mongocxx的文件夹,得要将后面的文件夹拷到上层目录,如下图:



对VS项目进行属性配置

打开VS软件,建立空项目,然后对其属性进行设置,如下图:









新建一个cpp文件,如下:

#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};

bsoncxx::builder::stream::document document{};

auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";

collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
system("pause");
}


然后执行即可。

备注:

1、Boost库安装生成:打开网页 https://sourceforge.net/projects/boost/files/boost-binaries/1.61.0/

选择 boost_1_60_0-msvc-14.0-64.exe 点击该安装文件,然后会生成boost的目录,本示例是在:D:\local\boost_1_60_0,其中 D:\local\boost_1_60_0是它的附加包含目录(头文件所在位置),D:\local\boost_1_60_0\lib64-msvc-14.0是它的Lib目录

2、在运行VS项目时,前确保MongoDB的服务器已经打开,可以连接

3、在生成lib和include时一定要选Release版,否则在运行VS项目时,会提示Debug Assertion false的错误。这个问题也深深的困扰了我好久!!!!

无力吐槽一下:生成这个Lib和include走了太多的弯路,版本没选对也会出现错误,这个坑太深了,所以今日分享出来,这样就不会装这个driver装太久。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MongoDB cxx-driver C++