1

This Question has been asked by different people about 3times by QLands: here and carnifrex: here and there. My problem is exactly like carnifrex's: I am trying to compile my application to connect to MYSQL(Mariadb) database without Mariadb installation i.e embedded server but I get this Error Below:

error: undefined reference to 'imp__ZN12QMYSQLDriverC1EP8st_mysqlP7QObject' collect2.exe:-1: error: error: ld returned 1 exit status

Unfortunately, carnifrex was not replied. Here's my Code

main.cpp:

#include "mainwindow.h"

#include <QApplication>
#include <QSql>
#include <QMessageBox>

#include "qsql_mysql.h"
#include <mysql.h>

bool createConnection(QMYSQLDriver *drver)
{
    QSqlDatabase db = QSqlDatabase::addDatabase(drver);
    db.setHostName("localhost");
    db.setDatabaseName("exama");
    db.setPort(3306);
    db.setUserName("root");
    db.setPassword("Adm1n16");

    if (!db.open()) {
        QMessageBox::warning(0, QObject::tr("Database Error"),
                             db.lastError().text());
        return false;
    }
    return true;
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MYSQL *mysql;

    static char *server_options[] = \
    {"mysql_test", "--defaults-file = C:/Program Files (x86)/MariaDB 10.1/data/my.cnf", NULL };
    int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;

    static char *server_groups[] = { "embedded", NULL };

    qDebug() << "Loading embedded";
    mysql_library_init(num_elements, server_options, server_groups);
    mysql = mysql_init(NULL);
    mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "embedded");
    mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

    mysql_real_connect(mysql, NULL,NULL,NULL, "database1", 0,NULL,0);

    QMYSQLDriver *drv = new QMYSQLDriver(mysql);

    if (!createConnection(drv))
        return 1;
    MainWindow w;
    w.show();

    return a.exec();

}

mysqlConnect.pro:

QT+=core gui sql\
        widgets \

greaterThan(QT_MAJOR_VERSION, 5): QT += widgets

INCLUDEPATH += "C:\Qt\Qt5.4\5.4\mingw491_32\include\QtSql\5.4.0\QtSql\private"
INCLUDEPATH += "C:\Program Files (x86)\MariaDB 10.1\include\mysql"

QMAKE_LIBDIR += "C:\Program Files (x86)\MariaDB 10.1\lib"

LIBS += -lmysql

TARGET = mySQLConnect
TEMPLATE = app

SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

QMAKE_CXXFLAGS+=-std=c++11

The compilation fails at this line in main.cpp:

QMYSQLDriver *drv = new QMYSQLDriver(mysql);

I am using Qt Creator 5.4.0 with MingW 4.9.1 32bit on x64 bit Windows 7 Machine. I will be grateful for any assistance granted.

5
  • Are you sure your mysql was also compiled with MinGW? Mixing C++ compilers on Windows rarely works. Commented Aug 8, 2016 at 9:44
  • Am not sure...mariadb comes with x86 and x64 installers for windows so I used the dll files distributed with the x86 bit installer... X64 dll files could not work... Is there a way I can away I can create my own libmysql.dll file with mingW? Commented Aug 10, 2016 at 6:33
  • I would first try use compile your work with the same compiler that mysql was compiled with, this takes much less time and will give you an answer what the real cause is. Commented Aug 10, 2016 at 7:51
  • I can't tell which compiler was used...all I know is that someone has succeeded compiling libmysql.dll with Qt creator.... I don't know but Qland seem to have compiled successfully only that the connection breaks down. Which compiler could be have been used? I will be happy for any assistance! Commented Aug 11, 2016 at 15:08
  • I have patiently waited for any substantial contribution about embedding mysql into Qt app with no avail. But I am still interested in this thread...is there no help out there? Commented Apr 26, 2018 at 10:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.