MongoDB C++ 初始化导致 Qt 崩溃

Posted

技术标签:

【中文标题】MongoDB C++ 初始化导致 Qt 崩溃【英文标题】:Qt break down by MongoDB C++ initialization 【发布时间】:2018-05-03 09:13:28 【问题描述】:

我刚开始在 Qt 中使用 MongoDB c++。

我已经按照MongoDB教程(https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/tutorial/)安装了Boost,MongoDB c驱动,MongoDB c++驱动,并在VS2017上成功测试了DB的连接。

但是,当我想在 Qt 上应用相同的代码时,出现了很多问题。

整体设定:

    Win10 Qt Creator v4.6.0 与 Qt v5.10.1 MSVC2017 64位 Microsoft Visual C++ 编译器 15.0 调试器 CDB x64 MongoDB v3.6.3 将 1.66.0 提升为 lib64-msvc-14.1 Mongo c驱动v1.9.3 Mongo cxx 驱动 r3.2.0

程序在初始化实例的时候就崩溃了,如下图: Break Down

只使用时程序也崩溃了

$mongocxx::instance inst()

编译为 using 时产生 C4930 警告

$mongocxx::instance inst()

我想我的问题有两个潜在原因 链接错误 错误版本的 boost/mongocxx 驱动程序 以下是 .pro 文件中的定义

    INCLUDEPATH += $$PWD/driver/c/include/libbson-1.0
    DEPENDPATH += $$PWD/driver/c/include/libbson-1.0
    LIBS += -L$$PWD/driver/c/lib -lbson-1.0
    LIBS += -L$$PWD/driver/c/lib -lbson-static-1.0


    INCLUDEPATH += $$PWD/driver/c/include/libmongoc-1.0
    DEPENDPATH += $$PWD/driver/c/include/libmongoc-1.0
    LIBS += -L$$PWD/driver/c/lib -lmongoc-1.0
    LIBS += -L$$PWD/driver/c/lib -lmongoc-static-1.0

    INCLUDEPATH += $$PWD/driver/c++/include/bsoncxx/v_noabi
    DEPENDPATH += $$PWD/driver/c++/include/bsoncxx/v_noabi
    LIBS += -L$$PWD/driver/c++/lib -lbsoncxx

    INCLUDEPATH += $$PWD/driver/c++/include/mongocxx/v_noabi
    DEPENDPATH += $$PWD/driver/c++/include/mongocxx/v_noabi
    LIBS += -L$$PWD/driver/c++/lib -lmongocxx        

    INCLUDEPATH += $$PWD/driver/boost_1_66_0
    DEPENDPATH += $$PWD/driver/boost_1_66_0
    LIBS += -L$$PWD/driver/boost_1_66_0/lib64-msvc-14.1 \
        -lboost_atomic-vc141-mt-gd-x64-1_66 \
        -lboost_atomic-vc141-mt-x64-1_66 \
        -lboost_bzip2-vc141-mt-gd-x64-1_66 \
        -lboost_bzip2-vc141-mt-x64-1_66 \
        -lboost_chrono-vc141-mt-gd-x64-1_66 \
        -lboost_chrono-vc141-mt-x64-1_66 \
        -lboost_container-vc141-mt-gd-x64-1_66 \
        -lboost_container-vc141-mt-x64-1_66 \
        -lboost_context-vc141-mt-gd-x64-1_66 \
        -lboost_context-vc141-mt-x64-1_66 \
        -lboost_coroutine-vc141-mt-gd-x64-1_66 \
        -lboost_coroutine-vc141-mt-x64-1_66 \
        -lboost_date_time-vc141-mt-gd-x64-1_66 \
        -lboost_date_time-vc141-mt-x64-1_66 \
        -lboost_fiber-vc141-mt-gd-x64-1_66 \
        -lboost_fiber-vc141-mt-x64-1_66 \
        -lboost_filesystem-vc141-mt-gd-x64-1_66 \
        -lboost_filesystem-vc141-mt-x64-1_66 \
        -lboost_graph-vc141-mt-gd-x64-1_66 \
        -lboost_graph-vc141-mt-x64-1_66 \
        -lboost_iostreams-vc141-mt-gd-x64-1_66 \
        -lboost_iostreams-vc141-mt-x64-1_66 \
        -lboost_locale-vc141-mt-gd-x64-1_66 \
        -lboost_locale-vc141-mt-x64-1_66 \
        -lboost_log-vc141-mt-gd-x64-1_66 \
        -lboost_log-vc141-mt-x64-1_66 \
        -lboost_log_setup-vc141-mt-gd-x64-1_66 \
        -lboost_log_setup-vc141-mt-x64-1_66 \
        -lboost_math_c99-vc141-mt-gd-x64-1_66 \
        -lboost_math_c99-vc141-mt-x64-1_66 \
        -lboost_math_c99f-vc141-mt-gd-x64-1_66 \
        -lboost_math_c99f-vc141-mt-x64-1_66 \
        -lboost_math_c99l-vc141-mt-gd-x64-1_66 \
        -lboost_math_c99l-vc141-mt-x64-1_66 \
        -lboost_math_tr1-vc141-mt-gd-x64-1_66 \
        -lboost_math_tr1-vc141-mt-x64-1_66 \
        -lboost_math_tr1f-vc141-mt-gd-x64-1_66 \
        -lboost_math_tr1f-vc141-mt-x64-1_66 \
        -lboost_math_tr1l-vc141-mt-gd-x64-1_66 \
        -lboost_math_tr1l-vc141-mt-x64-1_66 \
        -lboost_prg_exec_monitor-vc141-mt-gd-x64-1_66 \
        -lboost_prg_exec_monitor-vc141-mt-x64-1_66 \
        -lboost_program_options-vc141-mt-gd-x64-1_66 \
        -lboost_program_options-vc141-mt-x64-1_66 \
        -lboost_python-vc141-mt-gd-x64-1_66 \
        -lboost_python-vc141-mt-x64-1_66 \
        -lboost_random-vc141-mt-gd-x64-1_66 \
        -lboost_random-vc141-mt-x64-1_66 \
        -lboost_regex-vc141-mt-gd-x64-1_66 \
        -lboost_regex-vc141-mt-x64-1_66 \
        -lboost_serialization-vc141-mt-gd-x64-1_66 \
        -lboost_serialization-vc141-mt-x64-1_66 \
        -lboost_signals-vc141-mt-gd-x64-1_66 \
        -lboost_signals-vc141-mt-x64-1_66 \
        -lboost_stacktrace_noop-vc141-mt-gd-x64-1_66 \
        -lboost_stacktrace_noop-vc141-mt-x64-1_66 \
        -lboost_stacktrace_windbg-vc141-mt-gd-x64-1_66 \
        -lboost_stacktrace_windbg-vc141-mt-x64-1_66 \
        -lboost_stacktrace_windbg_cached-vc141-mt-gd-x64-1_66 \
        -lboost_stacktrace_windbg_cached-vc141-mt-x64-1_66 \
        -lboost_system-vc141-mt-gd-x64-1_66 \
        -lboost_system-vc141-mt-x64-1_66 \
        -lboost_thread-vc141-mt-gd-x64-1_66 \
        -lboost_thread-vc141-mt-x64-1_66 \
        -lboost_timer-vc141-mt-gd-x64-1_66 \
        -lboost_timer-vc141-mt-x64-1_66 \
        -lboost_type_erasure-vc141-mt-gd-x64-1_66 \
        -lboost_type_erasure-vc141-mt-x64-1_66 \
        -lboost_unit_test_framework-vc141-mt-gd-x64-1_66 \
        -lboost_unit_test_framework-vc141-mt-x64-1_66 \
        -lboost_wave-vc141-mt-gd-x64-1_66 \
        -lboost_wave-vc141-mt-x64-1_66 \
        -lboost_wserialization-vc141-mt-gd-x64-1_66 \
        -lboost_wserialization-vc141-mt-x64-1_66 \
        -lboost_zlib-vc141-mt-gd-x64-1_66 \
        -lboost_zlib-vc141-mt-x64-1_66 \
        -llibboost_atomic-vc141-mt-gd-x64-1_66 \
        -llibboost_atomic-vc141-mt-s-x64-1_66 \
        -llibboost_atomic-vc141-mt-sgd-x64-1_66 \
        -llibboost_atomic-vc141-mt-x64-1_66 \
        -llibboost_bzip2-vc141-mt-gd-x64-1_66 \
        -llibboost_bzip2-vc141-mt-s-x64-1_66 \
        -llibboost_bzip2-vc141-mt-sgd-x64-1_66 \
        -llibboost_bzip2-vc141-mt-x64-1_66 \
        -llibboost_chrono-vc141-mt-gd-x64-1_66 \
        -llibboost_chrono-vc141-mt-s-x64-1_66 \
        -llibboost_chrono-vc141-mt-sgd-x64-1_66 \
        -llibboost_chrono-vc141-mt-x64-1_66 \
        -llibboost_container-vc141-mt-gd-x64-1_66 \
        -llibboost_container-vc141-mt-s-x64-1_66 \
        -llibboost_container-vc141-mt-sgd-x64-1_66 \
        -llibboost_container-vc141-mt-x64-1_66 \
        -llibboost_context-vc141-mt-gd-x64-1_66 \
        -llibboost_context-vc141-mt-s-x64-1_66 \
        -llibboost_context-vc141-mt-sgd-x64-1_66 \
        -llibboost_context-vc141-mt-x64-1_66 \
        -llibboost_coroutine-vc141-mt-gd-x64-1_66 \
        -llibboost_coroutine-vc141-mt-s-x64-1_66 \
        -llibboost_coroutine-vc141-mt-sgd-x64-1_66 \
        -llibboost_coroutine-vc141-mt-x64-1_66 \
        -llibboost_date_time-vc141-mt-gd-x64-1_66 \
        -llibboost_date_time-vc141-mt-s-x64-1_66 \
        -llibboost_date_time-vc141-mt-sgd-x64-1_66 \
        -llibboost_date_time-vc141-mt-x64-1_66 \
        -llibboost_exception-vc141-mt-gd-x64-1_66 \
        -llibboost_exception-vc141-mt-s-x64-1_66 \
        -llibboost_exception-vc141-mt-sgd-x64-1_66 \
        -llibboost_exception-vc141-mt-x64-1_66 \
        -llibboost_fiber-vc141-mt-gd-x64-1_66 \
        -llibboost_fiber-vc141-mt-s-x64-1_66 \
        -llibboost_fiber-vc141-mt-sgd-x64-1_66 \
        -llibboost_fiber-vc141-mt-x64-1_66 \
        -llibboost_filesystem-vc141-mt-gd-x64-1_66 \
        -llibboost_filesystem-vc141-mt-s-x64-1_66 \
        -llibboost_filesystem-vc141-mt-sgd-x64-1_66 \
        -llibboost_filesystem-vc141-mt-x64-1_66 \
        -llibboost_graph-vc141-mt-gd-x64-1_66 \
        -llibboost_graph-vc141-mt-s-x64-1_66 \
        -llibboost_graph-vc141-mt-sgd-x64-1_66 \
        -llibboost_graph-vc141-mt-x64-1_66 \
        -llibboost_iostreams-vc141-mt-gd-x64-1_66 \
        -llibboost_iostreams-vc141-mt-s-x64-1_66 \
        -llibboost_iostreams-vc141-mt-sgd-x64-1_66 \
        -llibboost_iostreams-vc141-mt-x64-1_66 \
        -llibboost_locale-vc141-mt-gd-x64-1_66 \
        -llibboost_locale-vc141-mt-s-x64-1_66 \
        -llibboost_locale-vc141-mt-sgd-x64-1_66 \
        -llibboost_locale-vc141-mt-x64-1_66 \
        -llibboost_log-vc141-mt-gd-x64-1_66 \
        -llibboost_log-vc141-mt-s-x64-1_66 \
        -llibboost_log-vc141-mt-sgd-x64-1_66 \
        -llibboost_log-vc141-mt-x64-1_66 \
        -llibboost_log_setup-vc141-mt-gd-x64-1_66 \
        -llibboost_log_setup-vc141-mt-s-x64-1_66 \
        -llibboost_log_setup-vc141-mt-sgd-x64-1_66 \
        -llibboost_log_setup-vc141-mt-x64-1_66 \
        -llibboost_math_c99-vc141-mt-gd-x64-1_66 \
        -llibboost_math_c99-vc141-mt-s-x64-1_66 \
        -llibboost_math_c99-vc141-mt-sgd-x64-1_66 \
        -llibboost_math_c99-vc141-mt-x64-1_66 \
        -llibboost_math_c99f-vc141-mt-gd-x64-1_66 \
        -llibboost_math_c99f-vc141-mt-s-x64-1_66 \
        -llibboost_math_c99f-vc141-mt-sgd-x64-1_66 \
        -llibboost_math_c99f-vc141-mt-x64-1_66 \
        -llibboost_math_c99l-vc141-mt-gd-x64-1_66 \
        -llibboost_math_c99l-vc141-mt-s-x64-1_66 \
        -llibboost_math_c99l-vc141-mt-sgd-x64-1_66 \
        -llibboost_math_c99l-vc141-mt-x64-1_66 \
        -llibboost_math_tr1-vc141-mt-gd-x64-1_66 \
        -llibboost_math_tr1-vc141-mt-s-x64-1_66 \
        -llibboost_math_tr1-vc141-mt-sgd-x64-1_66 \
        -llibboost_math_tr1-vc141-mt-x64-1_66 \
        -llibboost_math_tr1f-vc141-mt-gd-x64-1_66 \
        -llibboost_math_tr1f-vc141-mt-s-x64-1_66 \
        -llibboost_math_tr1f-vc141-mt-sgd-x64-1_66 \
        -llibboost_math_tr1f-vc141-mt-x64-1_66 \
        -llibboost_math_tr1l-vc141-mt-gd-x64-1_66 \
        -llibboost_math_tr1l-vc141-mt-s-x64-1_66 \
        -llibboost_math_tr1l-vc141-mt-sgd-x64-1_66 \
        -llibboost_math_tr1l-vc141-mt-x64-1_66 \
        -llibboost_prg_exec_monitor-vc141-mt-gd-x64-1_66 \
        -llibboost_prg_exec_monitor-vc141-mt-s-x64-1_66 \
        -llibboost_prg_exec_monitor-vc141-mt-sgd-x64-1_66 \
        -llibboost_prg_exec_monitor-vc141-mt-x64-1_66 \
        -llibboost_program_options-vc141-mt-gd-x64-1_66 \
        -llibboost_program_options-vc141-mt-s-x64-1_66 \
        -llibboost_program_options-vc141-mt-sgd-x64-1_66 \
        -llibboost_program_options-vc141-mt-x64-1_66 \
        -llibboost_python-vc141-mt-gd-x64-1_66 \
        -llibboost_python-vc141-mt-s-x64-1_66 \
        -llibboost_python-vc141-mt-sgd-x64-1_66 \
        -llibboost_python-vc141-mt-x64-1_66 \
        -llibboost_random-vc141-mt-gd-x64-1_66 \
        -llibboost_random-vc141-mt-s-x64-1_66 \
        -llibboost_random-vc141-mt-sgd-x64-1_66 \
        -llibboost_random-vc141-mt-x64-1_66 \
        -llibboost_regex-vc141-mt-gd-x64-1_66 \
        -llibboost_regex-vc141-mt-s-x64-1_66 \
        -llibboost_regex-vc141-mt-sgd-x64-1_66 \
        -llibboost_regex-vc141-mt-x64-1_66 \
        -llibboost_serialization-vc141-mt-gd-x64-1_66 \
        -llibboost_serialization-vc141-mt-s-x64-1_66 \
        -llibboost_serialization-vc141-mt-sgd-x64-1_66 \
        -llibboost_serialization-vc141-mt-x64-1_66 \
        -llibboost_signals-vc141-mt-gd-x64-1_66 \
        -llibboost_signals-vc141-mt-s-x64-1_66 \
        -llibboost_signals-vc141-mt-sgd-x64-1_66 \
        -llibboost_signals-vc141-mt-x64-1_66 \
        -llibboost_stacktrace_noop-vc141-mt-gd-x64-1_66 \
        -llibboost_stacktrace_noop-vc141-mt-s-x64-1_66 \
        -llibboost_stacktrace_noop-vc141-mt-sgd-x64-1_66 \
        -llibboost_stacktrace_noop-vc141-mt-x64-1_66 \
        -llibboost_stacktrace_windbg-vc141-mt-gd-x64-1_66 \
        -llibboost_stacktrace_windbg-vc141-mt-s-x64-1_66 \
        -llibboost_stacktrace_windbg-vc141-mt-sgd-x64-1_66 \
        -llibboost_stacktrace_windbg-vc141-mt-x64-1_66 \
        -llibboost_stacktrace_windbg_cached-vc141-mt-gd-x64-1_66 \
        -llibboost_stacktrace_windbg_cached-vc141-mt-s-x64-1_66 \
        -llibboost_stacktrace_windbg_cached-vc141-mt-sgd-x64-1_66 \
        -llibboost_stacktrace_windbg_cached-vc141-mt-x64-1_66 \
        -llibboost_system-vc141-mt-gd-x64-1_66 \
        -llibboost_system-vc141-mt-s-x64-1_66 \
        -llibboost_system-vc141-mt-sgd-x64-1_66 \
        -llibboost_system-vc141-mt-x64-1_66 \
        -llibboost_test_exec_monitor-vc141-mt-gd-x64-1_66 \
        -llibboost_test_exec_monitor-vc141-mt-s-x64-1_66 \
        -llibboost_test_exec_monitor-vc141-mt-sgd-x64-1_66 \
        -llibboost_test_exec_monitor-vc141-mt-x64-1_66 \
        -llibboost_thread-vc141-mt-gd-x64-1_66 \
        -llibboost_thread-vc141-mt-s-x64-1_66 \
        -llibboost_thread-vc141-mt-sgd-x64-1_66 \
        -llibboost_thread-vc141-mt-x64-1_66 \
        -llibboost_timer-vc141-mt-gd-x64-1_66 \
        -llibboost_timer-vc141-mt-s-x64-1_66 \
        -llibboost_timer-vc141-mt-sgd-x64-1_66 \
        -llibboost_timer-vc141-mt-x64-1_66 \
        -llibboost_type_erasure-vc141-mt-gd-x64-1_66 \
        -llibboost_type_erasure-vc141-mt-s-x64-1_66 \
        -llibboost_type_erasure-vc141-mt-sgd-x64-1_66 \
        -llibboost_type_erasure-vc141-mt-x64-1_66 \
        -llibboost_unit_test_framework-vc141-mt-gd-x64-1_66 \
        -llibboost_unit_test_framework-vc141-mt-s-x64-1_66 \
        -llibboost_unit_test_framework-vc141-mt-sgd-x64-1_66 \
        -llibboost_unit_test_framework-vc141-mt-x64-1_66 \
        -llibboost_wave-vc141-mt-gd-x64-1_66 \
        -llibboost_wave-vc141-mt-s-x64-1_66 \
        -llibboost_wave-vc141-mt-sgd-x64-1_66 \
        -llibboost_wave-vc141-mt-x64-1_66 \
        -llibboost_wserialization-vc141-mt-gd-x64-1_66 \
        -llibboost_wserialization-vc141-mt-s-x64-1_66 \
        -llibboost_wserialization-vc141-mt-sgd-x64-1_66 \
        -llibboost_wserialization-vc141-mt-x64-1_66 \
        -llibboost_zlib-vc141-mt-gd-x64-1_66 \
        -llibboost_zlib-vc141-mt-s-x64-1_66 \
        -llibboost_zlib-vc141-mt-sgd-x64-1_66 \
        -llibboost_zlib-vc141-mt-x64-1_66

链接驱动有错误吗?

或者我使用的驱动版本不正确(但是使用VS2017正常)

有人遇到同样的问题吗? TKS

【问题讨论】:

【参考方案1】:

我在mongodb邮件列表上回复了你,但是你在这里做的很多事情都是不正确的。您不应该同时链接库的静态和动态版本。您不应该同时链接 boost 库的零售版本和调试版本等。您应该确保链接一致版本的 boost、QT 和 mongocxx/bsoncxx/libmongoc/libbson 库 w.r.t。调试/发布、静态/动态等。

我建议从首要原则开始,并根据需要逐步建立。从 mongocxx 项目中获取一个示例,并设置一个项目,该项目成功地针对您构建的驱动程序构建它。然后添加 QT 支持。

【讨论】:

以上是关于MongoDB C++ 初始化导致 Qt 崩溃的主要内容,如果未能解决你的问题,请参考以下文章

UIView 初始化覆盖导致 IBDesignable 崩溃

[QT]问题记录-控件初始化导致程序异常关闭

C++编码陷阱总结

Websphere 初始加载失败导致应用程序崩溃

qt qdebug正常releasd异常

为啥我的代码会崩溃?