PDA

View Full Version : How to use static mysql plugin



khikho
19th January 2009, 19:07
All

After googling failed, I am about to give up using static QT plugin. I hope someone who had solve this problem or know quickly and can point out what I am doing wrong. Here is a bit about the background.

I am trying to use the mysql driver offer in qt 4.4.x. I can successfull compile a version of shared QT and able to use the QSqlDatabase classes to work with the database. This work perfect :p.

However, I would like to compile the mysql driver and plugin into my application, this open up for the need to use static mysql driver. I followed the help and ran ./configure with -static and -qt-sql-mysql -plugin-sql-mysql -I/usr/include/mysql -L/usr/lib/mysql ... and a few more switches. The result is a libsqlmysql.a in the !QT/plugins/sqldrivers/*

My project file is set up as follow:

Template = app
TARGET = executable
QT += core sql
CONFIG += static
QTPLUGIN += qsqlmysql

source += main.cpp


The source file is as follow:



#include <QtCore>
#include <QSqlDatabase>
#include <QtPlugin>

QT_IMPORT_PLUGIN( qsqlmysql )

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

QSqlDatabase db = QSqlDatabase::addDatabase( "MYSQL" );

return 0;
}

all of this is a simple exercise to load the SQL driver.

Here is the problem :crying:.

When compiling with the QT_IMPORT_PLUGIN I have multiple problem including "undefined reference to mysql_server_end" together with many other sql_ related function :confused:.

if I take off the QT_IMPORT_PLUGIN then the it compile fine, the exec is large which suggested that it compiled the qsqlmysql static lib into it. However, at run time, it complain of no MYSQL driver loaded :confused:.

Through out googling, I see many question like this and the common solution is recompile and use it as share object. I really need to know a correct setup of static object and hope that this will be the answer for many other to come.

If you know the answer, please help, I am sure that many other are waiting for the answer as well..

OS is redhat
compiler is g++

wysota
19th January 2009, 19:19
Do you have those static plugins built?

khikho
19th January 2009, 19:21
hey thanks for replying,

I do have the -qt-sql-mysql driver and plugin specified. all seems correct.

wysota
19th January 2009, 19:56
I'm asking if the static plugin file is the sqldrivers directory. BTW. -qt-sql-mysql and -plugin-sql-mysql are exclusive. The first compiles mysql support directly into Qt library whereas the second one builds a static plugin. Please check if any of your Qt libraries depends on the mysql client library.

khikho
19th January 2009, 19:58
ah, sorry about that,

according to the documentation, it seems that I do. I have the libqsqlmysql.a in the plugin/sqldrivers directory.

khikho
19th January 2009, 20:13
Being a newbies I don't know how to check if any library depends on mysqlclient. after reading other forum, I assume that it does, other suggested to include -L/usr/lib/mysql -lmysqlclient

after doing that, I have a different error:
" /usr/bin/ld: skipping incompatible /usr/lib/mysql/libmysqlclient.so when searching for -lmysqlclient
/usr/bin/ld: skipping incompatible /usr/lib/mysql/libmysqlclient.a when searching for -lmysqlclient
/usr/bin/ld: cannot find -lmysqlclient"

I checked and there exist under /usr/lib/sql both the libmysqlclient.so and .a

this stuff is beyond my knowledge, I hope you can help.

thanks

wysota
19th January 2009, 21:10
Run ldd on every Qt library (QtCore and QtSql being the most obvious ones) and see if "libmysqlclient" is there.

khikho
19th January 2009, 21:44
I finally figured this out, for future user, here is correct way to do this step by step:

How to use static plugin in qt: example for mysql

1) ./configure -static -plugin-sql-mysql ... and many other switch as you wish,

2) make sure your project file look as follow:

Template = app
TARGET = executable
QT += core sql
CONFIG += static
QTPLUGIN += qsqlmysql
LIBS += -L/usr/lib64/mysql -lmysqlclient ( use /usr/lib/mysql for 32 bit system )
source += main.cpp


3) make sure your file have the macro that look as followed:

#include <QtCore>
#include <QSqlDatabase>
#include <QtPlugin>

QT_IMPORT_PLUGIN( qsqlmysql )

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

QSqlDatabase db = QSqlDatabase::addDatabase( "MYSQL" );
qDebug() << QSqlDatabase::drivers();


return 0;
}


This will give you a working example to push ahead.



4) for using mysql driver as share object, just recompile qt as follow
./configure -qt-sql-mysql ... and many other switch

5) use QSqlDatabase as in step 3 without the PLUGIN macro.