1. 程式人生 > >MongoDb Windows 環境下 c++ driver 安裝流程

MongoDb Windows 環境下 c++ driver 安裝流程

  1. 安裝boost 1.60
    位置: c:\boost_1_60_0

  2. 先編譯 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

  3. 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

  <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>
  1. 工程main.cpp
#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 };

成功編譯並執行