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();
}
2
  • As a tip, you can search on Google this error message removing your specific keywords, entering the query: undefined reference to mysql_init. Commented Jan 25, 2011 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 Commented Jan 29, 2011 at 15:31

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

9 Comments

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.
mysql support usually comes as a plugin though and thus shouldn't produce a compile time but a runtime error.
i followed the steps in this tutorial: doc.qt.nokia.com/4.7/… and got stuck, everything seems to work but the libraries won't show up in the specified directory
Could you provide more info? You must do something wrong if it isn't working.
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.
|
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).

Comments

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.

Comments

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.