First checkout the version you want to build. Always build from a particular tag, since HEAD may be a work in progress. For example, to build version 0.8.1, run:
git checkout v0.8.1
Then follow the build steps below.
Before compiling, you should note the following compile options.
For big-endian support, define:
If your compiler has a plain bool type, define:
Alternatively, if you must include stdbool.h to get bool, define:
If you’re not using C99, then you must choose your 64-bit integer type by defining one of these:
If you’re building the driver on posix-compliant platforms, including on OS X and Linux, then you can build with make.
To compile the driver, run:
This will build the following libraries:
You can install the libraries with make as well:
And you can run the tests:
You can even build the docs:
By default, make will build the project in c99 mode. If you want to change the language standard, set the value of STD. For example, if you want to build using the ANSI C standard, set STD to c89:
Once you’ve built and installed the libraries, you can compile the sample with gcc like so:
gcc --std=c99 -I/usr/local/include -L/usr/local/lib -o example docs/examples/example.c -lmongoc
If you want to statically link the program, add the -static option:
gcc --std=c99 -static -I/usr/local/include -L/usr/local/lib -o example docs/examples/example.c -lmongoc
Then run the program:
To clean up objects:
To clobber products, includes clean:
You may also build the driver using the Python build utility, SCons. This is required if you’re building on Windows. Make sure you’ve installed SCons, and then from the project root, enter:
This will build static and dynamic libraries for both BSON and for the the driver as a complete package. It’s recommended that you build in C99 mode with optimizations enabled:
Once you’ve built the libraries, you can compile a program with gcc like so:
gcc --std=c99 -static -Isrc -o example docs/example/example.c libmongoc.a
On Posix systems, you may also install the libraries with scons:
To build the docs:
When building the driver on Windows, you must use the Python build utility, SCons. Install Python 2 (NOT Python 3) from www.python.org, making sure to install the 32-bit version and NOT the 64-bit version. Install SCons from www.scons.org. For your compiler, we recommend that you use Visual Studio. If you don’t have Visual Studio, a free version is available. Search for Visual Studio C++ Express to find it.
If you’re running on 32-bit Windows, you must compile the driver in 32-bit mode:
If getaddrinfo and friends aren’t available on your version of Windows, you may compile without these features like so:
scons --m32 --standard-env
The original goal of the MongoDB C driver was to provide a very basic library capable of being embedded anywhere. This goal is now evolving somewhat given the increased use of the driver. In particular, it now makes sense to provide platform-specific features, such as socket timeouts and DNS resolution, and to return platform-specific error codes.
To that end, we’ve organized all platform-specific code in the following file:
This implements the interface defined in env.h.
If you want to compile with the generic, platform implementation, you have to do so explicity. In SCons:
The driver itself has no dependencies, but one of the tests shows how to create a JSON-to-BSON converter. For that test to run, you’ll need JSON-C.
Make sure that you’re running mongod on 127.0.0.1 on the default port (27017). The replica set test assumes a replica set with at least three nodes running at 127.0.0.1 and starting at port 30000. Note that the driver does not recognize ‘localhost’ as a valid host name.
To compile and run the tests with SCons:
You may optionally specify a remote server:
scons test --test-server=188.8.131.52
You may also specify an alternate starting port for the replica set members:
scons test --test-server=184.108.40.206 --seed-start-port=40000