安装boost 1.60
位置: c:\boost_1_60_0
下载安装mongo c dirver
http://mongoc.org/?jmp=docs#documentation
http://mongoc.org/libmongoc/current/installing.html
Building on Windows
先编译 libbson
进入libsson的目录:
cd mongo-c-driver-1.9.5\src\libbson
mongo-c-driver-1.9.5是安装压缩包的解压目录
cmake 创建 libbson 的编译环境
“c:\Program Files\CMake\bin\cmake.exe” -G “Visual Studio 15 2017 Win64” “-DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver” “-DCMAKE_BUILD_TYPE=Release”
注意这里确定了cmake的详细路径, Visual Studio 的版本.
完成了cmake创建编译环境后,开始正式编译 libbson
msbuild.exe /p:Configuration=Release ALL_BUILD.vcxproj
msbuild.exe /p:Configuration=Release INSTALL.vcxproj
下面进入mongo c driver的目录:
cd mongo-c-driver-1.9.5
cmake 创建 mongo c driver 的编译环境
“c:\Program Files\CMake\bin\cmake.exe” -G “Visual Studio 15 2017 Win64” “-DENABLE_SSL=WINDOWS” “-DENABLE_SASL=SSPI” “-DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver” “-DCMAKE_PREFIX_PATH=C:\mongo-c-driver” “-DCMAKE_BUILD_TYPE=Release”
开始编译 mongo c driver
msbuild.exe /p:Configuration=Release ALL_BUILD.vcxproj
msbuild.exe /p:Configuration=Release INSTALL.vcxproj
最后所有mongo c driver文件安装在C:\mongo-c-driver
下载安装mongo c++ driver
https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/#installing-the-mongocxx-driver
git 最新mongo cxx driver
git clone https://github.com/mongodb/mongo-cxx-driver.git –branch releases/stable –depth 1
cmake 创建 mongo c++ driver 编译环境
“c:\Program Files\CMake\bin\cmake.exe” -G “Visual Studio 15 2017 Win64” -DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver -DCMAKE_PREFIX_PATH=c:\mongo-c-driver -DBOOST_ROOT=c:\boost_1_60_0
这里注意Boost路径(“本人电脑为 c:\boost_1_60_0”)必须设置正确。
开始编译 mongo c++ driver
msbuild.exe ALL_BUILD.vcxproj
msbuild.exe INSTALL.vcxproj
修改使用mongodb c++ driver 工程的 vcxproj 文件
https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/#installing-the-mongocxx-driver
Compiling with MSVC
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>c:\local\boost_1_59_0\;C:\mongo-cxx-driver\include\mongocxx\v_noabi;C:\mongo-cxx-driver\include\bsoncxx\v_noabi;C:\mongo-c-driver\include\libmongoc-1.0;C:\mongo-c-driver\include\libbson-1.0;$(IncludePath)</IncludePath>
<LibraryPath>c:\mongo-c-driver\lib\;c:\mongo-cxx-driver\lib\;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>MONGOCXX_STATIC;BSONCXX_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>mongocxx.lib;bsoncxx.lib;mongoc-static-1.0.lib;bson-1.0.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
#include "stdafx.h"
#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;
}
}
上面这个工程,本人无法成功编译.
注意根据官方文档 mongocxx::uri{} 实际上等价于 mongocxx::uri(“mongodb://localhost:27017”)
上面这个工程我们编译报错为:
Error LNK2001 unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const mongocxx::v_noabi::uri::k_default_uri" (?k_default_uri@uri@v_noabi@mongocxx@@2V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B)
修改为
mongocxx::instance inst{};
//mongocxx::client conn{ mongocxx::uri{} };
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client conn{ uri };
成功编译并运行