I’ve blown some serious time over the last couple of days getting the MongoDB C++ driver/library built, and being able to link against it. First, I had problems compiling the MongoDB driver when trying to link against the Boost library. Then there were problems compiling a simple program (the tutorial program from the driver documentation) that leverage the MongoDB library. The exact problems really were too numerous to document here, and to be honest, I was pretty tunnel-visioned on getting everything to work correctly. So, I’m afraid I don’t have all the errors I fought with here for you to search against.
With that in mind, I hope this might help some people with general issues getting these libraries off the ground and working together. In particular, I found the steps listed in one StackOverflow answer to be very helpful. In general, no attempts with gcc/g++ were of any use. Once I started throwing clang at the problem, I started to see results. Here’s a rundown of my process:
- Download Boost (the latest version at the time of writing is 1.55.0).
- Decompress your Boost download and drop into a terminal in the decompressed folder. I used the instructions from the Boost documentation with a few modifications:
1234567891011121314151617181920212223242526272829303132333435363738394041$ ./bootstrap.sh-n Building Boost.Build engine with toolset darwin...tools/build/v2/engine/bin.macosxx86_64/b2-n Detecting Python version...2.7-n Detecting Python root.../System/Library/Frameworks/Python.framework/Versions/2.7-n Unicode/ICU support for Boost.Regex?...not found.Backing up existing Boost.Build configuration in project-config.jam.13Generating Boost.Build configuration in project-config.jam...Bootstrapping is done. To build, run:./b2To adjust configuration, edit 'project-config.jam'.Further information:- Command line help:./b2 --help- Getting started guide:http://www.boost.org/more/getting_started/unix-variants.html- Boost.Build documentation:http://www.boost.org/boost-build2/doc/html/index.html$ ./b2 toolset=clang cxxflags="-stdlib=libstdc++" linkflags="-stdlib=libstdc++" installlink.jam: No such file or directoryPerforming configuration checks- 32-bit : no (cached)- 64-bit : no (cached)...snip...common.copy /usr/local/lib/libboost_wave.a...failed updating 58 targets......skipped 12 targets......updated 10820 targets...
In short, that’s just:
123$ ./bootstrap.sh && \./b2 toolset=clang cxxflags="-stdlib=libstdc++" \linkflags="-stdlib=libstdc++" install
- Download and decompress the MongoDB C++ driver.
- Back in the terminal, in the MongoDB C++ driver decompressed folder:
12345678910$ scons --cc=clang --cxx=clang++ \--dbg=on --full --sharedclient \--use-system-boost install-mongoclientscons: Reading SConscript files ...scons version: 2.3.0...snip...install_name_tool -id /usr/local/lib/libmongoclient.dylib /usr/local/lib/libmongoclient.dylibscons: done building targets.
- Save tutorial.cpp from the MongoDB C++ driver documentation. In the same directory:
1234$ clang++ tutorial.cpp -stdlib=libstdc++ -pthread \-lmongoclient -lboost_thread -lboost_filesystem \-lboost_program_options -lboost_system \-o tutorial
In general, I had to pay particularly close attention to these requirements (thanks again to that SO answer!):
- Mongo DB uses exactly the same version of Boost library and headers that you are using.
- Make sure that they are looking on exactly the same headers.
- Make sure libmongoclient and Boost library are both compiled with the clang compiler.
- Make sure you link with the correct versions of the Boost library.
- Make sure you use the same C++ flags for all compilations (i.e. stuff like C++0x)