How can I build a program using c++ driver of mong

2019-01-12 07:38发布

问题:

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 Ultimate 2012 (x64 Release target project)

I performed the following steps.

  • I installed Ptython 2.7.2, Python for Windows extensions and Scons 2.3.0.
  • I installed boost 1.54.0. I perfomed 'boostrap' and 'b2 -a --build-type=complete --address-model=64 --link=static --runtime-link=static'
  • I download mongodb-linux-x86_64-v2.4-latest.tgz.
  • I added the following texts to SConstruct.

    env.Append(CPPPATH=['C:\\boost\\boost_1_54_0'])
    env.Append(LIBPATH=['C:\\boost\\boost_1_54_0\\stage\\lib'])
    env.Append(CPPDEFINES=["_UNICODE"])
    env.Append(CPPDEFINES=["UNICODE"])
    
  • I performed 'scons mongoclient'.

  • I made a project of Visual Studio C++.
  • I set 'Release' and 'x64' in the project.
  • In Property pages/Configuration Properties/C C++/General/Additional Include Directories, I set 'C:\boost\boost_1_54_0' and 'C:\mongo\mongo-cxx-driver-v2.4\src'.
  • In Property pages/Configuration Properties/Linker/General/Additional Library Directories, I set 'C:\boost\boost_1_54_0\stage\lib' and 'C:\mongo\mongo-cxx-driver-v2.4'.
  • In Property pages/Configuration Properties/Linker/Input/Additional Dependences, I set 'mongoclient.lib','ws2_32.lib' and 'psapi.lib'.
  • In Property pages/Configuration Properties/Linker/Input/Ignore Specific Default Libraries, I set 'msvcprt.lib' and 'LIBCMT.lib'.
  • In Property pages/Configuration Properties/C C++/Preprosessor/Preprocessor Definitions, I set '_CRT_SECURE_NO_WARNINGS'.
  • In Property pages/Configuration Properties/C C++/Code Generation/Runtime Library, I set 'Multi-threaded (/MT)'.

I made a program using the driver. However, the build solution of the program has failed.

1>mongoclient.lib(stringutils.obj) : error LNK2001: unresolved external symbol __security_check_cookie
1>mongoclient.lib(initializer_dependency_graph.obj) : error LNK2001: unresolved external symbol __security_check_cookie
1>mongoclient.lib(initializer.obj) : error LNK2001: unresolved external symbol __security_check_cookie
....
 fatal error LNK1120: 154 unresolved externals

If would help me if you pointed out anything strange about the steps.

Thank you.

回答1:

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.



回答2:

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;