Qt: undefined reference to 'mysql_something @nr'

I am really stuck right now, every time I try to compile my C ++ program, I get the output as follows:

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'

Scrolling all day to find workarounds, tutorials, whatever, reading tutorials, uninstalling mingw, mysql server, qt and reinstalling everything. I uninstalled qt again and built it from the source code ... converted libmysql.dll with 0.3 mingw-utils reimp.exe and dlltools.exe, etc. Nothing helped.

before using QT (notepad ++ and mingw only), I also had linker warnings telling me something about the stdcall-fixup-disable error, but compiled and processed programs.

later I again reinstalled everything that I think the current setup does not work better than the other setups before I do not even know that I created qt from the source. Is there a simple (regular, uncomplicated) way to get Qt, MinGW, C ++, MySQL5.5 to work together?

edit2: I fixed the code now that I got it to work, this is the smallest piece of code to start 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();
}
+3
source share
4 answers

It looks like you need to add the mysql library to the build process. If you are using the Qt qmake system, then something like:

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

in the .pro file for the project.

+3
source

Qt's binary distribution does not contain a built-in MySQL plugin. You must add it manualy. You must also add it QT += sqlto your .pro file.

+3
source

MYSQL:

GCC:

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

Alternatively, you can add a directory -Lto indicate where the MySQL libraries are installed.

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

+1
source

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.

0
source

Source: https://habr.com/ru/post/1787767/


All Articles