I’m probably just being dense, but there must be something I have overlooked
trying to get a program using the QtSql module to compile and link
successfully using kdevelop. Here is the source code:
#include <QCoreApplication> #include <Qt/QtSql>
int main(int argc, char *argv])
{
QCoreApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase(“QMYSQL”);
db.setHostName(“localhost”);
db.setDatabaseName(“test”);
bool ok = db.open();
printf("Database %s open successfully!
", (ok) ? “did” : “did not”);
return 0;
}
You can’t get much simpler than that! The example from which I’ve drawn has
a difference in line 2. It does “#include <QtSql>”. If I do that, I get
compile errors. I managed to get past those by adding the subdirectory as
shown. The program compiles ok, but now the linker cannot resolve the
references, giving:
main.cpp:(.text+0x6e): undefined reference to QSqlDatabase::addDatabase(QString const&, QString const&)' main.cpp:(.text+0xb9): undefined reference to QSqlDatabase::setHostName(QString const&)’
main.cpp:(.text+0xf0): undefined reference to QSqlDatabase::setDatabaseName(QString const&)' main.cpp:(.text+0x105): undefined reference to QSqlDatabase::open()’
main.cpp:(.text+0x12b): undefined reference to QSqlDatabase::~QSqlDatabase()' main.cpp:(.text+0x1a4): undefined reference to QSqlDatabase::~QSqlDatabase()’
main.cpp:(.text+0x1d5): undefined reference to
`QSqlDatabase::~QSqlDatabase()’
I assume there must be a missing library, but I have run out of guesses.
Can someone please point me in the right direction so I can get on with my
project?
What if you just use a text editor instead of KDevelop?
Save the file, run
qmake -project
This will create the pro file, edit it to add the QT += sql line. Then run qmake filename.pro, and this should generate a Makefile. Finally issue the make command…
Code:
#include <QCoreApplication>
#include <Qt/QtSql>
int main(int argc, char *argv])
{
QCoreApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
bool ok = db.open();
printf("Database %s open successfully!
", (ok) ? "did" : "did not");
return 0;
}
larry@Microtel:~/Projects/test2> make
/usr/bin/qmake -unix -o Makefile test2.pro
g++ -o test2
main.o -L/usr/lib -lQtGui -L/usr/lib -L/usr/X11R6/lib -lpng -lSM -lICE -lXi -lXrender -lXrandr -lXfixes -lXcursor -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -lrt -ldl -lpthread
main.o: In function main': main.cpp:(.text+0x33): undefined reference to QSqlDatabase::defaultConnection’
main.cpp:(.text+0x6e): undefined reference to QSqlDatabase::addDatabase(QString const&, QString const&)' main.cpp:(.text+0xb9): undefined reference to QSqlDatabase::setHostName(QString const&)’
main.cpp:(.text+0xf0): undefined reference to QSqlDatabase::setDatabaseName(QString const&)' main.cpp:(.text+0x105): undefined reference to QSqlDatabase::open()’
main.cpp:(.text+0x12b): undefined reference to QSqlDatabase::~QSqlDatabase()' main.cpp:(.text+0x1a4): undefined reference to QSqlDatabase::~QSqlDatabase()’
main.cpp:(.text+0x1d5): undefined reference to
`QSqlDatabase::~QSqlDatabase()’
collect2: ld returned 1 exit status
make: *** [test2] Error 1
I am convinced that the problem with the #include is somehow related to
this. All of the examples show that I should do this: #include <QtSql>
However, the compiler cannot find that file, giving me:
compiling main.cpp (g++)
src/main.cpp:23:25: error: QtSql: No such file or directory
If I change it to this: #include <Qt/QSql>
the compile runs successfully, but then I get the link errors. It makes me
think the include and link paths are incorrect. That would seem to explain
why doing make from the command line also fails.
Thoughts?
silenuz wrote:
>
> It compiles fine for me.
>
> What if you just use a text editor instead of KDevelop?
>
> Save the file, run
> qmake -project
>
> This will create the pro file, edit it to add the QT += sql line. Then
> run qmake filename.pro, and this should generate a Makefile. Finally
> issue the make command…
>
> Code:
>
>
> Code:
> --------------------
> #include <QCoreApplication>
> #include <Qt/QtSql>
> int main(int argc, char *argv])
> {
> QCoreApplication app(argc, argv);
> QSqlDatabase db = QSqlDatabase::addDatabase(“QMYSQL”);
> db.setHostName(“localhost”);
> db.setDatabaseName(“test”);
> bool ok = db.open();
> printf("Database %s open successfully!
", (ok) ? “did” : “did not”);
> return 0;
> }
> --------------------
>
>
>
> pro file:
>
>
> Code:
> --------------------
> ######################################################################
> # Automatically generated by qmake (2.01a) Sat Nov 21 10:48:46 2009
> ######################################################################
>
> TEMPLATE = app
> TARGET =
> DEPENDPATH += .
> INCLUDEPATH += .
>
> # Input
> SOURCES += test.cpp
> QT += sql
>
>
> --------------------
>
>
<bewildered>
Incidentally, I’ve been going back over my other kdevelop projects. All but
one of them compiles and links successfully. That project (which used to
compile okay) is exhibiting the same odd behavior.
#include <QDialog> #include <QTcpSocket>
This now gives me:
client.h:26:23: error: QTcpSocket: No such file or directory
</bewildered>
> I’m probably just being dense, but there must be something I have
> overlooked trying to get a program using the QtSql module to compile and
> link
> successfully using kdevelop. Here is the source code:
>
> #include <QCoreApplication>
> #include <Qt/QtSql>
> int main(int argc, char *argv])
> {
> QCoreApplication app(argc, argv);
> QSqlDatabase db = QSqlDatabase::addDatabase(“QMYSQL”);
> db.setHostName(“localhost”);
> db.setDatabaseName(“test”);
> bool ok = db.open();
> printf("Database %s open successfully!
", (ok) ? “did” : “did not”);
> return 0;
> }
>
> You can’t get much simpler than that! The example from which I’ve drawn
> has
> a difference in line 2. It does “#include <QtSql>”. If I do that, I get
> compile errors. I managed to get past those by adding the subdirectory as
> shown. The program compiles ok, but now the linker cannot resolve the
> references, giving:
>
> main.cpp:(.text+0x6e): undefined reference to
> `QSqlDatabase::addDatabase(QString const&, QString const&)’
main.cpp:(.text+0xb9): undefined reference to QSqlDatabase::setHostName(QString const&)' > main.cpp:(.text+0xf0): undefined reference to > QSqlDatabase::setDatabaseName(QString const&)’
main.cpp:(.text+0x105): undefined reference to QSqlDatabase::open()' > main.cpp:(.text+0x12b): undefined reference to > QSqlDatabase::~QSqlDatabase()’
main.cpp:(.text+0x1a4): undefined reference to QSqlDatabase::~QSqlDatabase()' > main.cpp:(.text+0x1d5): undefined reference to > QSqlDatabase::~QSqlDatabase()’
>
> I assume there must be a missing library, but I have run out of guesses.
> Can someone please point me in the right direction so I can get on with my
> project?
>
> TIA!
>
> SUSE 11.0
> kdevelop 3.5.1
> Qt 4.4
The problem definitely was missing libraries. The instructions in the Qt
documentation instruct you to place the following in your .pro file:
QT += sql
Unfortunately, this does absolutely nothing I can detect! The makefile’s
generated by qmake are identical whether or not this directive is present.
The solution is to add the following to the .pro file:
INCLUDEPATH += /usr/include/QtSql
LIBS += -lQtSql
It even works after removing the useless “QT += sql” directive!