Building and using the MongoDB C++ driver/library

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:

  1. Download Boost (the latest version at the time of writing is 1.55.0).
  2. 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:

    In short, that’s just:
  3. Download and decompress the MongoDB C++ driver.
  4. Back in the terminal, in the MongoDB C++ driver decompressed folder:
  5. Save tutorial.cpp from the MongoDB C++ driver documentation. In the same directory:

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)

Trouble installing the JSON gem

I just got a replacement machine from Apple (literally the only time I’ve ever been pleased with Apple’s customer service). The recent iOS upgrade for my iPhone and iPad required me to upgrade to Xcode 4.3. Needless to say, all of this gave me quite a headache when it came to reinstalling Ruby, RVM and various gems. Either way, I was able to get Ruby 1.9.3-p125 installed using RVM, but the issues with gem installation have persisted (with a handful of gems, at least).

One of those problems came yesterday when trying to install the JSON gem. Here’s what went down:

Several StackOverflow questions (like this and this) that I’d found said this was an issue with my Xcode installation. One guy said this “just works” with the OS X 10.7.3 update. Not true at all, guy.

Lots of discussions pointed me to this Github repo. I’m not interested in installing just the command-line tools with which Xcode ships–I already use Xcode. Other places said to install the command line tools from the Downloads tab in Xcode preferences. I’d already done that. This post seemed to have the answer. It gave me two options:

  • Use the separate standalone gcc installer
  • Uninstall XCode 4.2 and Install XCode 4.1

Well, I have to use XCode 4.3, so rolling back wasn’t an option. The discussion of a separate standalone gcc compiler didn’t apply either–gcc --version told me I had GCC 4.2.1 installed. Ah-ha! Back in my error output, I saw this:

That’s really all it was. So, this fixed it for me:

Hope that helps someone…