如何在Qt Creator中使用MongoDB C驱动程序?

时间:2013-03-03 06:59:30

标签: qt mongodb-c

我想用MongoDB开发一个Qt GUI应用程序作为后端数据库。所以我需要使用MongoDB C驱动程序或C ++驱动程序。

说实话,在Windows下构建C ++驱动程序很困难。当我做" scons"时,它无法找到提升,我已经安装了提升。我不知道为什么。

所以我选择了MongoDB C驱动程序。当我做了#34; scons"时,它运行良好并生成了四个文件(bson.lib,bson.dll,mongoc.lib,mongoc.dll)。但我并不确切知道如何使用这些库和DLL来使其在Qt Creator中运行。

1 个答案:

答案 0 :(得分:2)

我还没有完成C驱动程序,但我正在使用Qt Creator进行C ++编程。您需要在项目中包含boost库,并且 - 对于我下载的MongoDB客户端C ++版本 - 它们需要是Boost 1.49库,不多也不少。下载它,只是让它构建所有的库,即使你只需要其中的四个库。以下是我的Qt Creator .pro文件中的相关代码,请注意我的C:/ MongoDB文件夹中的所有内容都是从MongoDB源下载的,或者至少是由直接下载的scons构建的。

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
}

请注意,Qt在针对静态C ++运行时构建时会出现异常,因此最好遵循我给出的建议here并将驱动程序包装在针对静态运行时构建的非Qt C ++ dll中,然后在主Qt应用程序中使用该DLL,它将针对动态运行时构建。

另请注意,我必须手动将winsock和帮助库复制到根文件夹并手动包含它们,因为Qt Creator不会接受“Program Files(x86)”路径,因为它有空格。

我意识到这不是一个“Mongo C”的答案,但是你确实提到你只是使用C驱动程序,因为让C ++的工作失败,所以我想我会分享我所知道的。