Baarn
Baarn

Reputation: 673

qt: undefined reference to 'mysql_something@nr'

I am really stuck right now, everytime I try to compile my c++ programm I get output like this:

release/dialog.o:dialog.cpp:(.text+0x9e): undefined reference to `mysql_init@4'
release/dialog.o:dialog.cpp:(.text+0xe1): undefined reference to `mysql_real_connect@32'

Browsing the whole day to find workarounds, tutorials, whatever, reading tutorials, uninstalling mingw, mysql server, qt and installing everything again. I deleted qt again and build it from source... converted libmysql.dll with the 0.3 mingw-utils reimp.exe and dlltools.exe and so on, nothing helped.

before using QT (just notepad++ and mingw), I had linker warnings aswell, telling me something bout stdcall-fixup-disable, but the programms compiled and worked.

later i will reinstall everything again i think, the current setup isn't working better than other installations before i dont even know what i did building qt from source. is there any easy (normal, uncomplicated) way to get Qt, MinGW, C++, MySQL5.5 working together?

edit2: i corrected the code now, that i got it to work, this is some minimal code snippet to begin with:

#include <QtCore/QCoreApplication>
#include <QMYSQLDriver>
#include <qsqldatabase.h>
#include <QtSql>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    // insert other connection options here

    if(!db.open()){
        // dp not open, add some debug text and stuff, exit or retry
    }

    //db should be open at this place, add code here

    return app.exec();
}

Upvotes: 3

Views: 2648

Answers (4)

vz0
vz0

Reputation: 32923

If you are using Eclipse CDT you need to add the MySQL library to the linker settings. Go to Project Properties -> GCC++ linker ->libraries, and add mysql.

Upvotes: 0

Maciej
Maciej

Reputation: 3723

Binary distribution of Qt doesn't contain MySQL plugin built-it. You have to add it manualy You should also add QT += sql in your .pro file.

Upvotes: 3

Michael Burr
Michael Burr

Reputation: 340198

It sounds like you need to add the mysql library to the build process. If you're using Qt's qmake system, something like:

LIBS += -L/wherever/the/mysql/lib/is -lmysql

in the .pro file for the project.

Upvotes: 3

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

Link it with MYSQL libraries:

If you are using GCC:

$ gcc -Wall your_file.c - o your_program -lmysql

Additionally you might want to add a -L directory to include the place where you have MySQL's libraries installed.

It would help if you provide your exact compilation line/mechanism (Makefile, IDE, etc).

Upvotes: 1

Related Questions