1. Introduction
Tossing MongoDB for a few hours finally had a result. Uh! Now let's summarize it briefly.
In fact, my requirement is very simple, is to call the library function of MongoDB in C + + code, that is to get mongoclient.lib. Originally want to download directly, can not find on the internet, feel that recompile is not troublesome, so the source code compiled. Can under the wrong source, made up a half-day also can't make out, dizzy!
In fact, refer to the articles of netizens, here download the source Mongodb-src-r2.6.3.zip. Compile the command is also very simple, everyone said direct input: SCons mongoclient.lib can, can keep popping such errors:
SCons: * * * does not know what to make File target ' mongoclient.lib ' (C:\mongo-maste
R\mongoclient.lib). Stop.
The results do not know how to find Mongoclient.lib in the new version is no longer the target, has been moved to another source Mongo-cxx-driver-legacy.zip, the address here. However, in mongo-src, like Mongo,mongod or directly can be compiled successfully, seemingly do not need the boost library
2. Preparatory work
2.1 Download and install Mongo-win32.msi
In order to save time, I downloaded the relevant EXE file directly like Mogo.exe,mogod.exe
2.2 Installing Python,scons,boost in turn
For more information, refer to the link address I gave you. I'll highlight some of the things I've found:
2.2.1 Some people say that mongoclient only link boost Static library, I find that is not the case, when you specify--dynamic-windows,--sharedclient parameters, you need to give the boost dynamic Lib library directory, not *-s.lib
2.2.2 When the specified--dynamic-windows,--sharedclient parameter is not specified, it seems that I don't have to give the Boost library directory, I'm not sure, anyway, I didn't give a libboost_date_time-vc110-mt-s-1_ like that. Path to files such as 53.lib
3. Compiling mongoclient
From the vs2012 command line into the Mongo-cxx-driver-legacy directory, execute:
SCons install-mongoclient--cpppath=boost Header file path
Results direct generation of libmongoclient-s.lib,128m in Build\install\lib
Of course, you can also add parameters such as--dynamic-windows,--sharedclient,--dbg to generate the corresponding dynamic libraries, including debug and release versions. Detailed description can be seen here. As a result, the following files are in my directory:
To tell the truth, I really did not understand the differences between these documents, I have been messy ...
4. Testing
I have to say, this step is really hanging out, there are two important steps I guessed.
Originally the code is very simple, connected on the Mongod.exe process to open the server on the line, do not know how the matter, the Netizen did not I say that two steps, is not the new version of the issue of updating, or I overlooked something?
The code is as follows:
#include <iostream> #include <cstdlib> #include <winsock2.h> #include "mongo\client\dbclient.h" Using namespace Std;void run () {MONGO::D bclientconnection con;con.connect ("127.0.0.1:27017");} int main () {Wsadata wsadata;if (WSAStartup (Makeword (), &wsadata)! = 0) {return-1;} try{run (); cout << "Conn OK "<< Endl;} catch (const MONGO::D bexception &e) {cout << "caught" << e.what () << Endl;} GetChar (); return exit_success;}
before running, needless to say, be sure to add boost and Mongoclient header files and library files. Note that I add the Dynamic library lib path, should be dynamic link, so also need to add boost DLL path and Mongoclient-gd.dll path to the path, and also add Ws2_32.lib library, As for the boost library and the Mongoclient library, you can write the directory directly without writing the library name because there is an automatic link mechanism.
Here are my two major changes, just compared to the files of netizens:
1, add winsock2.h before dbclient.h header file, because in vs2012 if no error: "You must include the Windows and Windows Sockets headers before Dbclient.h "
2, at the beginning of the main function, I added the WSAStartup 5 lines of code, because if not added, the GETADDR call in a file will be an exception: "WSAStartup () failed"
Can not say that the regular code is wrong, can only say for a variety of reasons, in this case need to do so in order to compile and run normally.
Run is simple, first start Mogod-dbpath d:\data, the default port at 27017, and then start the above client, output conn OK. Done!
5. Reference website
http://blog.csdn.net/mniwc/article/details/8590528
http://ppgunjack.iteye.com/blog/985904
Http://database.51cto.com/art/201106/272526.htm
The compilation and use of MongoDB and mongoclient under Win7