The primary target for this article is to install the mongodb C++ driver for development with C++ projects.
There is a newer branch C++ 11 based driver at this point but it’s better to start with the stable legacy one.
In a normal fresh Linux system the following steps has to be go through to meet the driver building prerequisites. For a full instruction please refer to “https://github.com/mongodb/mongo-cxx-driver/wiki/Download-and-Compile-the-Legacy-Driver“.
Download and build/install Boost (Boost.org or Official Guide)
Driver require a boost version >= 1.49
tar -xzvf ./boost_1_xx_x.tar.gz cd ./boost_1_xx_x/ ./bootstrap.sh ./b2 sudo ./b2 install |
Install Python if needed (Python.org)
Driver require a Python version > 2.0
Download and install Scons (Scons.org)
Install by RPM package or build from source.
rpm -uvh scons-x.x.x-x.noarch.rpm |
Git is also needed to clone the mongo-cxx-driver
sudo yum install git |
Or
sudo apt-get install git |
Build and Install the MongoDB C++ Driver
git clone git@github.com:mongodb/mongo-cxx-driver.git cd ./mongo-cxx-driver git checkout legacy scons sudo scons install |
Important: After this step the include and lib were generated but NOT locate in proper location. Use a prefix option to build and install or move everything under /mongo-cxx-driver/build/install/ into /usr/local/
mv ./build/install/* /usr/local/ |
Time to build and test a sample program
Check if you have $LD_LIBRARY_PATH in valid value by
echo $LD_LIBRARY_PATH |
And if nothing is showed go export the lib path by
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH |
Build the test program with
g++ tutorial.cpp -pthread -lmongoclient -lboost_thread -lboost_system -lboost_regex -o tutorial |
Here is a great example to connect and insert into MongoDB: https://gist.github.com/brandondiamond/1528272. Note that a initialize() has to be added into the source to avoid a coredump.
int main() { mongo::client::initialize(); ... } |
All set!
Everything is verified with:
- Boost_1_55_0
- Scons-2.3.5-1.noarh.rpm
- Mongo-cxx-driver branch legacy