How can I build a program using c++ driver of mongodb?

前端 未结 2 661
不知归路
不知归路 2020-12-04 00:15

Please tell me how to build a program using c++ driver of mongodb. No flames about my bad English.

My Environment

  • Windows7 64bit
  • Visual Studio
相关标签:
2条回答
  • 2020-12-04 00:55

    How to build MongoDB C++ driver

    This solution succeeded on a machine with the following characteristics:

    1. Windows XP SP3 32-bit
    2. Visual Studio Express 2010 (VC 10)

    I used D:\MongoDBcplusplusClient as a working directory (I installed there all the prerequisites).

    Process:

    • Step 1

    Download MongoDB C++ driver:

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

    You can make a clone using Git or download it as a .zip file (I did the second).You will get a file like mongo-cxx-driver-legacy.zip. Extract it to the folder mongo-cxx-driver-legacy inside your working directory.

    • Step 2

    Download Boost prebuilt windows binaries. ATTENTION!!! You should use a specific version of Boost. In my case version 1.52 did the trick. You can download it from here:

    http://boost.teeks99.com/

    I downloaded the boost_1_52_0-vc32-bin.exe self-extracting exe. Put it on your working directory and run it. It will create a folder (something like lib32) that will contain the boost binaries (.lib and .dll files)

    • Step 3

    Download Boost source code (.h files). Of course these should be from the same version as in Step 2. I downloaded them from here:

    http://sourceforge.net/projects/boost/files/boost/1.52.0/

    You will get a file boost_1_52_0.zip which you can extract at boost_1_52_0 folder.

    • Step 4

    Download Python. In this example I downloaded version 2.7.9 and specifically the Windows x86 MSI installer from here:

    https://www.python.org/downloads/release/python-279/

    • Step 5

    Download Scons from here:

    http://www.scons.org/download.php

    I downloaded the Windows installer (scons-2.3.4-setup.exe) and installed Scons at the Python directory (in my case C:\Python27).

    • Step 6

    Download msinttypes from here:

    https://code.google.com/p/msinttypes/

    (You should include these header files to the project that uses the driver)

    • Step 7

    Go to Start->Run… and in the Run box write cmd. In the opened command prompt window navigate to the folder at which you extracted mongo driver at Step 1. In my case I did: cd D:\ D:\MongoDBcplusplusClient\mongo-cxx-driver-legacy\mongo-cxx-driver-legacy

    • Step 8

    Build the driver using Scons. In the directory you navigated at Step 7 write:

    scons

    --prefix=D:\MongoDBcplusplusClient\mongo-cxx-driver-legacy\mongo-cxx-driver-legacy

    --cpppath=D:\MongoDBcplusplusClient\boost_1_52_0\boost_1_52_0

    --libpath= D:\MongoDBcplusplusClient\lib32

    --win-version-min=xpsp3 install

    and hit Enter.

    The --prefix flag specifies the target directory at which the .lib file of the driver will be created, --cpppath specifies the folder at which the Boost header files are located and the --libpath the path to Boost .lib files. Of course you should change the path to yours. A file named libmongoclient-s.lib will be created at the --prefix/lib path. If you want to build the driver with debugging enabled you should use the following command:

    scons

    --prefix=D:\MongoDBcplusplusClient\mongo-cxx-driver-legacy\mongo-cxx-driver-legacy

    --cpppath=D:\MongoDBcplusplusClient\boost_1_52_0\boost_1_52_0

    --libpath= D:\MongoDBcplusplusClient\lib32

    --win-version-min=xpsp3

    --dbg=on install

    A file named libmongoclient-sgd.lib will be created at the --prefix/lib path.

    • Step 9

    At Windows Explorer navigate to the folder at which MongoDB C++ driver is installed, go into the subfolder lib (in my case this was D:\MongoDBcplusplusClient\mongo-cxx-driver-legacy\mongo-cxx-driver-legacy\lib) and rename the file libmongoclient-s.lib to mongoclient.lib and the libmongoclient-sgd.lib to mongoclient-gd.lib.

    • Step 10

    Open Visual Studio 2010 Express and open the project at which you want to use the MongoDB C++ driver. You should specify the dependencies. Right click on project’s name at solution explorer (left column) and hit Properties. Go to C/C++ → General and at Additional Include Directories add:

    a) Boost header files directory (in my case D:\MongoDBcplusplusClient\boost_1_52_0\boost_1_52_0)

    b) MongoDB C++ driver header files directory (in my case D:\MongoDBcplusplusClient\mongo-cxx-driver-legacy\mongo-cxx-driver legacy\include)

    c) Cstdint types header files directory (in my case :\MongoDBcplusplusClient\msinttypes-r26 )

    • Step 11

    Go to Linker → General and at Additional Library Directories add:

    a) Boost .lib files directory (in my case D:\MongoDBcplusplusClient\lib32)

    b) MongoDB C++ driver .lib files directory (in my case D:\MongoDBcplusplusClient\mongo-cxx-driver-legacy\mongo-cxx-driver-legacy\lib)

    After these steps the project that uses the driver will be successfully built both in release and debug configurations.

    0 讨论(0)
  • 2020-12-04 01:07

    Simple solution use vcpkg.

    Download vcpkg follow the instructions as mentioned on git. https://github.com/Microsoft/vcpkg

    Step 1 C:\vcpkg>.\vcpkg search mongodb

    you will see something like that

    mongo-c-driver 1.6.2-1 Client library written in C for MongoDB.

    mongo-cxx-driver 3.1.1-1 MongoDB C++ Driver.

    Step 2 C:.\vcpkg search mongodb install mongo-cxx-driver

    then grab cup of coffee ....

    Stap 3

    C:\vcpkg>.\vcpkg integrate install

    Done..

    Note Prerequisites:

    Windows 10, 8.1, or 7

    Visual Studio 2017 or Visual Studio 2015 Update 3

    simply import

       #include <cstdint>
       #include <iostream>
       #include <vector>
       #include <bsoncxx/json.hpp>
       #include <mongocxx/client.hpp>
       #include <mongocxx/stdx.hpp>
       #include <mongocxx/uri.hpp>
    
         using bsoncxx::builder::stream::close_array;
         using bsoncxx::builder::stream::close_document;
         using bsoncxx::builder::stream::document;
         using bsoncxx::builder::stream::finalize;
         using bsoncxx::builder::stream::open_array;
         using bsoncxx::builder::stream::open_document;
    
    0 讨论(0)
提交回复
热议问题