3

I want to develop a Qt GUI application with MongoDB serving as the back-end database. So I need to use MongoDB C driver or C++ driver.

Truth be told, it's kind of difficult to build a C++ driver under Windows. When I do "scons", it can't find boost and I have installed boost. I don't know why.

So I choose MongoDB C driver. When I did "scons", it went all well and generated four files (bson.lib, bson.dll, mongoc.lib, mongoc.dll). But I don't know exactly how to use these libs and DLL's to make it work in Qt Creator.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Ivan Jobs
  • 71
  • 1
  • 4

1 Answers1

2

I haven't done the C driver, but I'm in the process of doing the C++ one with Qt Creator. You need to include the boost libraries in your project, and--for the version of MongoDB client C++ that I downloaded anyway--they need to be the Boost 1.49 libraries, no more and no less. Download it and just have it build all the libraries, even though you only need about four of them for this. The following is the relevant code from my Qt Creator .pro file, and note that everything in my C:/MongoDB folder is downloaded from the MongoDB source, or at least built by scons from that direct download.

INCLUDEPATH += C:/MongoDB/src   \
 C:/MongoDB/src/mongo/client  \
 C:/MongoDB/src/third_party/boost  \
 C:/MongoDB/src/third_party/boost/boost   \
 C:/MongoDB/src/mongo    \
 C:/MongoDB/src/third_party/boost/boost/algorithm  \
 C:/MongoDB/src/third_party/boost/boost/asio  \
 C:/MongoDB/src/third_party/boost/boost/bind  \
 C:/MongoDB/src/third_party/boost/boost/concept \
 C:/MongoDB/src/third_party/boost/boost/config \
 C:/MongoDB/src/third_party/boost/boost/container \
 C:/MongoDB/src/third_party/boost/boost/date_time \
 C:/MongoDB/src/third_party/boost/boost/detail  \
 C:/MongoDB/src/third_party/boost/boost/exception  \
 C:/MongoDB/src/third_party/boost/boost/filesystem \
 C:/MongoDB/src/third_party/boost/boost/function   \
 C:/MongoDB/src/third_party/boost/boost/functional \
 C:/MongoDB/src/third_party/boost/boost/integer    \
 C:/MongoDB/src/third_party/boost/boost/io    \
 C:/MongoDB/src/third_party/boost/boost/iterator   \
 C:/MongoDB/src/third_party/boost/boost/math    \
 C:/MongoDB/src/third_party/boost/boost/move    \
 C:/MongoDB/src/third_party/boost/boost/mpl     \
 C:/MongoDB/src/third_party/boost/boost/numeric  \
 C:/MongoDB/src/third_party/boost/boost/optional  \
 C:/MongoDB/src/third_party/boost/boost/pending  \
 C:/MongoDB/src/third_party/boost/boost/preprocessor \
 C:/MongoDB/src/third_party/boost/boost/program_options\
 C:/MongoDB/src/third_party/boost/boost/random  \
 C:/MongoDB/src/third_party/boost/boost/range   \
 C:/MongoDB/src/third_party/boost/boost/regex   \
 C:/MongoDB/src/third_party/boost/boost/smart_ptr \
 C:/MongoDB/src/third_party/boost/boost/spirit   \
 C:/MongoDB/src/third_party/boost/boost/system    \
 C:/MongoDB/src/third_party/boost/boost/test     \
 C:/MongoDB/src/third_party/boost/boost/thread    \
 C:/MongoDB/src/third_party/boost/boost/tuple     \
 C:/MongoDB/src/third_party/boost/boost/type_traits \
 C:/MongoDB/src/third_party/boost/boost/typeof   \
 C:/MongoDB/src/third_party/boost/boost/units   \
 C:/MongoDB/src/third_party/boost/boost/unordered \
 C:/MongoDB/src/third_party/boost/boost/utility   \

DEFINES += _UNICODE   \
    SYM_STATICLIB

QMAKE_CFLAGS_RELEASE += /MT
QMAKE_CXXFLAGS_RELEASE += /MT
QMAKE_CFLAGS_DEBUG += /MTd
QMAKE_CXXFLAGS_DEBUG += /MTd

LIBS += -L$$PWD/../../../../../../MongoDB/src/third_party -lWS2_32
LIBS += -L$$PWD/../../../../../../MongoDB/src/third_party -lDbgHelp

CONFIG(debug, debug|release) {
    LIBS += -LC:\MongoDB\build\win32\debug\client_build -lmongoclient
    LIBS += -L$$PWD/../../../../../../Boost/1.49/bin.v2/libs/thread/build/msvc-10.0/debug/link-static/runtime-link-static/threading-multi/ -llibboost_thread-vc100-mt-sgd-1_49
    LIBS += -L$$PWD/../../../../../../Boost/1.49/bin.v2/libs/date_time/build/msvc-10.0/debug/link-static/runtime-link-static/threading-multi/ -llibboost_date_time-vc100-mt-sgd-1_49
    LIBS += -L$$PWD/../../../../../../Boost/1.49/bin.v2/libs/system/build/msvc-10.0/debug/link-static/runtime-link-static/ -llibboost_system-vc100-sgd-1_49
    LIBS += -L$$PWD/../../../../../../Boost/1.49/bin.v2/libs/filesystem/build/msvc-10.0/debug/link-static/runtime-link-static/ -llibboost_filesystem-vc100-sgd-1_49
}

CONFIG(release, debug|release) {
    LIBS += -LC:\MongoDB\build\win32\release\client_build -lmongoclient
    LIBS += -L$$PWD/../../../../../../Boost/1.49/bin.v2/libs/thread/build/msvc-10.0/release/link-static/runtime-link-static/threading-multi/ -llibboost_thread-vc100-mt-s-1_49
    LIBS += -L$$PWD/../../../../../../Boost/1.49/bin.v2/libs/date_time/build/msvc-10.0/release/link-static/runtime-link-static/threading-multi/ -llibboost_date_time-vc100-mt-s-1_49
    LIBS += -L$$PWD/../../../../../../Boost/1.49/bin.v2/libs/system/build/msvc-10.0/release/link-static/runtime-link-static/ -llibboost_system-vc100-s-1_49
    LIBS += -L$$PWD/../../../../../../Boost/1.49/bin.v2/libs/filesystem/build/msvc-10.0/release/link-static/runtime-link-static/ -llibboost_filesystem-vc100-s-1_49
}

Note that Qt is known to misbehave when built against static C++ runtimes, so it's probably best to follow the advice I was given here and wrap the driver in a non-Qt C++ dll which is built against static runtimes, and then use that dll inside the main Qt app which would be built against the dynamic runtimes.

Also note that I had to manually copy the winsock and help libs into a root folder and include them manually because Qt Creator wouldn't accept the "Program Files (x86)" path since it had spaces in it.

I realize this isn't a "Mongo C" answer, but you did mention that you're only using the C driver out of frustration getting the C++ one to work, so I thought I'd share what I know.

Community
  • 1
  • 1
kanders84152
  • 1,251
  • 10
  • 17