2 / 2
Nov 2024

Hi, I’m working on a project in Qt Creator and I’m trying to integrate mongocxx into the project. I downloaded the mongo c driver (1.17.0), boost (1.7.3), and the mongocxx driver (3.6.0) according the documentation and everything seemed to build successfully (I used “Visual Studio 15 2017 Win64” with the -G flag). In my .pro file, I added include paths and libs for the driver, and my project manages to build and execute successfully with mongocxx includes in my header files. However, as soon as I try to add any code that uses mongocxx (like the line mongocxx::instance inst{};), the program crashes when I try to execute it and will continue to crash until I comment out the code and clean/rebuild the project (error message only says “The program has unexpectedly finished”).

It seems like I didn’t connect my project to the mongocxx driver correctly, but I’m not sure what else to check, so any suggestions would be appreciated! I’m using a kit with Qt 5.15.0 MSVC 2015 64-bit to build and run my project.

test code:

#include "database_utils.h" #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> #include <iostream> // reference: https://docs.mongodb.com/drivers/cxx bool connectToDatabase() { std::cout <<"in connect!" << std::endl; // execution fails if this code is uncommented (and print statement above is not reached) // try { // mongocxx::instance inst{}; // This should be done only once. // mongocxx::client conn{ // mongocxx::uri{ // "mongodb+srv://admin:dPJk8Uj0idkQ5Kqc@csas-cluster.w3smq.mongodb.net/csas?retryWrites=true&w=majority" // } // }; // } catch (...) { // std::cout << "caught" << std::endl; // } return false; }

.pro file:

QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 INCLUDEPATH += \ C:/Users/craft/Downloads/mongo-cxx-driver-r3.6.0/include/mongocxx/v_noabi \ C:/Users/craft/Downloads/mongo-cxx-driver-r3.6.0/include/bsoncxx/v_noabi \ C:/Users/craft/Downloads/boost_1_73_0 \ LIBS += \ -LC:/Users/craft/Downloads/mongo-cxx-driver-r3.6.0/lib -lmongocxx \ -LC:/Users/craft/Downloads/mongo-cxx-driver-r3.6.00/lib -lbsoncxx SOURCES += \ appointment.cpp \ database_utils.cpp \ doctor.cpp \ doctorview.cpp \ main.cpp \ loginui.cpp \ patient.cpp \ patientview.cpp \ schedule.cpp \ user.cpp HEADERS += \ appointment.h \ database_utils.h \ doctor.h \ doctorview.h \ loginui.h \ patient.h \ patientview.h \ schedule.h \ user.h FORMS += \ doctorview.ui \ loginui.ui \ patientview.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target`
4 years later

i have the same problem as you, i use qt try to connect to mongodb,but it told me an error: “An exception occurred: Invalid UTF-8 in URI: an invalid MongoDB URI was provided” ,do you have serve it,please