3

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();
}
Baarn
  • 673
  • 11
  • 23
  • As a tip, you can search on Google this error message removing your specific keywords, entering the query: *undefined reference to mysql_init*. – vz0 Jan 25 '11 at 19:24
  • ok i think i'll accept one answer now, because my problem is now no longer a unlinked lib but a runtime error – Baarn Jan 29 '11 at 15:31

4 Answers4

3

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.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • this helped, my program compiled now. but the problem continue =/ mysql_init(my); works fine but it crashes on mysql_real_connect(); i edit my posting with the tiny bit of code i have – Baarn Jan 29 '11 at 11:49
3

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.

Maciej
  • 3,685
  • 1
  • 19
  • 14
  • 1
    It should be noted that if you go this route you need to use the QtSQL API which is database agnostic. You won't be able to use `mysql_init()`. Using QtSQL is a good choice however. – jonescb Jan 25 '11 at 19:47
  • mysql support usually comes as a plugin though and thus shouldn't produce a compile time but a runtime error. – Frank Osterfeld Jan 25 '11 at 21:10
  • i followed the steps in this tutorial: http://doc.qt.nokia.com/4.7/sql-driver.html#how-to-build-the-mysql-driver-for-mingw-users and got stuck, everything seems to work but the libraries won't show up in the specified directory – Baarn Jan 26 '11 at 09:09
  • Could you provide more info? You must do something wrong if it isn't working. – Maciej Jan 29 '11 at 08:42
  • i found my error, prolly because i am on windows 7 and didn't use the admin shell for everything. Now I do have the four files, but i don#t know where to put them. atm i still get linking errors. – Baarn Jan 29 '11 at 10:29
  • ok, i can use #include now, but i still get the undefined reference error – Baarn Jan 29 '11 at 11:11
  • as stated on another answer my program compiles now, but crashes on runtime. – Baarn Jan 29 '11 at 11:56
  • Could you provide more information on this error? Have you added `QT += sql` to .pro file? Also try using instead. – Maciej Jan 30 '11 at 16:47
  • it works now, i added QT += sql and LIBS += -L/wherever/the/mysql/lib/is -lmysql to the file. no more crashes on runtime :) main problem with the linkers were, that you have to use the special functions the qt plugin provides, not those you might be used to from standard mysql coding. – Baarn Feb 01 '11 at 16:22
1

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).

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
0

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.

vz0
  • 32,345
  • 7
  • 44
  • 77