Giter Club home page Giter Club logo

mongol's Introduction

Installing the mongocxx driver on Windows

Also in  🇧🇷

Motivation

I tried to install Mongocxx using the MongoDB official documentation but I found errors during installation and they require you to use Visual Studio. Since there are no good guides on how to install and use mongocxx drivers on Windows without using Visual Studio I decided to make my own using MinGW.

Author’s Note

If you can, please don't use Windows. There are a lot Linux distributions out there and I'm certain that there is one which suits you. All the errors I found during the installation were because I was using Windows.
Only use Windows for development if you must test code on a Windows machine.

Table of Contents

Softwares

MinGW

I used the MinGW that comes with Qt.

CMake

I used the CMake 3.7.2.

mongo-c-driver

As the official documentation says, the mongocxx driver builds on top of the MongoDB C driver. So you need to install it first.

I downloaded the release version mongo-c-driver 1.6.3.

$ tar xzf mongo-c-driver-1.6.3.tar.gz

libbson

Let's start by installing libbson, a dependency of the C driver.

$ cd mongo-c-driver-1.6.3/src/libbson
$ cmake -G "MinGW Makefiles" "-DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver"

cmake will output this.

$ C:\Qt\Tools\mingw491_32\bin\mingw32-make.exe
$ C:\Qt\Tools\mingw491_32\bin\mingw32-make.exe install

make will output this. make install will output this.

Now you should see at your -DCMAKE_INSTALL_PREFIX (in my case C:\mongo-c-driver) files like these.

MongoDB C driver

Now let's do the same for the MongoDB C driver.

Go to mongo-c-driver-1.6.3 folder.

$ cd ../..
$ cmake -G "MinGW Makefiles" "-DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver" "-DBSON_ROOT_DIR=C:\mongo-c-driver" -DENABLE_SASL:BOOL=OFF

cmake will output this.

Note that I tried to install without using -DENABLE_SASL:BOOL=OFF, but I found too many errors.

$ C:\Qt\Tools\mingw491_32\bin\mingw32-make.exe
$ C:\Qt\Tools\mingw491_32\bin\mingw32-make.exe install

make will output this. make install will output this.

Now you should see at your -DCMAKE_INSTALL_PREFIX (in my case C:\mongo-c-driver) files like these.

mongo-cxx-driver

Look on the mongocxx releases page for a link to the release tarball for the version you wish you install. For example, to download version 3.1.1 (I used this one):

$ curl -OL https://github.com/mongodb/mongo-cxx-driver/archive/r3.1.1.tar.gz
$ tar -xzf r3.1.1.tar.gz
$ cd mongo-cxx-driver-r3.1.1/build
$ cmake -G "MinGW Makefiles" -DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver -DLIBBSON_DIR=C:\mongo-c-driver -DLIBMONGOC_DIR=C:\mongo-c-driver ..

cmake will output this.

C:/Qt/Tools/mingw491_32/bin/mingw32-make.exe
C:/Qt/Tools/mingw491_32/bin/mingw32-make.exe install

make will output this. make install will output this.

Now you should see at your -DCMAKE_INSTALL_PREFIX (in my case C:\mongo-cxx-driver) files like these.

PATH

Put C:\mongo-cxx-driver\bin;C:\mongo-c-driver\bin on PATH.

Test your installation

You can use the code provided in the official documentation, or you can find the code below:

#include <iostream>

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

int main(int, char**) {
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{}};

    bsoncxx::builder::stream::document document{};

    auto collection = conn["testdb"]["testcollection"];
    document << "hello" << "world";

    collection.insert_one(document.view());
    auto cursor = collection.find({});

    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }
}

Compile

To compile I used this command line:

g++ --std=c++11 test.cpp -o test -IC:/mongo-cxx-driver/include/mongocxx/v_noabi -IC:/mongo-cxx-driver/include/bsoncxx/v_noabi -LC:/mongo-cxx-driver/lib -lmongocxx -lbsoncxx

mongol's People

Contributors

abarichello avatar losoliveirasilva avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

abarichello

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.