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

说实话,在 Windows 下构建 C++ 驱动程序有点困难。当我执行“scons”时,它找不到 boost,我已经安装了 boost。我不知道为什么。

所以我选择了MongoDB C驱动。当我执行“scons”时,一切顺利并生成了四个文件(bson.lib、bson.dll、mongoc.lib、mongoc.dll)。但我不知道如何使用这些库和 DLL 使其在 Qt Creator 中工作。

请您参考如下方法:

我还没有完成 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++ 驱动程序工作的挫败感,所以我想我会分享我所知道的。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!