How to add a lib to a qt project
Hello,
I'm using qt4 on a Windows 7 system and I'm trying to get access to a PostgreSQL 9 DB.
That's why I'm trying to add libpg.lib (with libpg-fe.h) to my project. But I'm doning something wrong.
I added "LIBS += -lpg" to my projectfile *.pro.
Code:
QT += core gui
TARGET = SQL_BSP
TEMPLATE = app
LIBS += -lpg
SOURCES += main.cpp\
mainwindow.cpp \
postgresqldb.cpp
HEADERS += mainwindow.h \
libpq-fe.h \
postgres_ext.h \
postgresqldb.h
FORMS += mainwindow.ui
OTHER_FILES += \
libpq.lib
When I start compiling my project I get the following error message:
Code:
cannot find -lpg
collect2: ld returned 1 exit status
I'm waiting for your help.
Thanks,
Alex
Re: How to add a lib to a qt project
Where is this library located ? You need to point the linker to the library directory as well:
Code:
LIBS += -L"path/to/library/directory"
Re: How to add a lib to a qt project
Thanks for your reply.
My libpg.lib is located in the same directory as the whole qt-project.
Just to be sure, I added information of the whole path of the lib to the *.pro file.
Code:
LIBS += -lpg
LIBS += -L"D:\Qt-Projekte\SQL_BSP"
But I still got the same error messages.
Re: How to add a lib to a qt project
Sorry, I guess you want the static linkage:
Code:
LIBS += "D:/Qt-Projekte/SQL_BSP/libpg.lib"
Re: How to add a lib to a qt project
If I add the codeline you write and remove my two codelines I still get one errormessage.
Code:
collect2: ld returned 1 exit status
I've no idea what that error means.
Re: How to add a lib to a qt project
Is it the only error message you get, this single line ? Aren't there any message like "undefined reference to ..." ?
Are you sure that the path to .lib file is correct ?
Be sure to remove LIBS += -lpg from the .pro file and add only the LIBS += "path/to/lib/libpg.lib".
Post the whole .pro file. Try to do a clean build as well.
Did you built the "libpg.lib" yourself ? If you've downloaded it, are you sure it's good one for your compiler ?
---
if you use QtCreator, try to scroll up in the compiler output window, maybe you just see the last line.
Re: How to add a lib to a qt project
Yes, it's the only error message. In addition I get a warning.
Code:
Qmake does not support build directories below the source directory
This is my SQL_BSP.pro file.
Code:
QT += core gui
TARGET = SQL_BSP
TEMPLATE = app
LIBS += -L"D:/Qt-Projekte/SQL_BSP/libpg.lib"
SOURCES += main.cpp\
mainwindow.cpp \
postgresqldb.cpp
HEADERS += mainwindow.h \
libpq-fe.h \
postgres_ext.h \
postgresqldb.h
FORMS += mainwindow.ui
OTHER_FILES += \
libpq.lib
Yes, I am sure that every file of my project is located in my specified path.
I got the lib from a friend of mine. He used it to communicate to a postgres db with a Visual C++ project (without MFC).
If it is not compatible to my compiler, where can I get the right *.lib? I've read about qsqlpsql4.dll and qsqlpsql4.lib in the web. These files are in the plugin folder of qt, but there is no header file existing. So I don't know how to use them.
Re: How to add a lib to a qt project
Quote:
These files are in the plugin folder of qt
Great, so you can use them - add QT += sql to .pro file and include <QtSql> in order to use Qt database framework. There are many examples how to use it in Qt examples directory, and you have nice demo of sqlbrowser implemented with Qt as well.
Re: How to add a lib to a qt project
Thank you for your help so far.
My SQL_BSP.pro now looks like this:
Code:
QT += core gui
QT += sql
TARGET = SQL_BSP
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
OTHER_FILES += \
libpq.lib
I tried to get access to the sql db like this:
Code:
#include <QtSql>
#include <QSqlError>
#include <QSqlDriver>
#include <QSqlDriverPlugin>
dbAlex.setHostName("localhost");
dbAlex.setPort(5433);
dbAlex.setUserName("postgres");
dbAlex.setPassword("***");
dbAlex.setDatabaseName("QT");
dbAlex.open("postgres", "***");
if (dbAlex.isOpen() == true)
m_sConnState = "true";
else
m_sConnState = "false";
m_sConnState = dbError.text();
ui->lineEdit->setText(m_sConnState);
The message in my lineEdit now is "driver not loaded".
I now, I habe to work with QSqlDriver and QSqlDriverPlugin, but I have no idea how.
Re: How to add a lib to a qt project
You can remove the OTHER_FILES += libpg.lib, because you are going to use Qt plugin for accessing database.
You have the qsqlpsql4.dll, so you should be able to use it, try to copy this file to your applications directory, or another place where windows searches for dlls.
There is a QSqlDatabase::drivers () method, it returns a list of all available drivers.
Re: How to add a lib to a qt project
okay, I copied the "qsqlpsql4.dll" and "qsqlpsql.lib" to my project directory and added it in the *.pro file:
Code:
LIBS += -L"qsqlpsql4.lib"
In my *.cpp I asked for available drivers:
The Result ist slDrivers is:
QSQLITE
QODBC3
QODBC
But no PSQL or something similar.
Re: How to add a lib to a qt project
Quote:
LIBS += -L"qsqlpsql4.lib"
this line makes no sense - use -L switch when you want to add a directory to linker search path, the same I haven't noticed in your .pro file posted before:
Quote:
LIBS += -L"D:/Qt-Projekte/SQL_BSP/libpg.lib"
There should be no "-L" here either.
You don't have to add qt sql plugins to pro file, because you want to load them at runtime.
Try to see if the plugin is visible if you call QApplication::addLibraryPath(path) with path=<"directory with qt psql plugin"> after creating QApplication instance.
Re: How to add a lib to a qt project
Sorry, but I'm really a beginner in Qt.
I think I need more information, about what I have to do.
Maybe it will help me if there is an easy example for using postgres with qt.
Re: How to add a lib to a qt project
Before you can use the PostgreSQL plugin for Qt you will have to build it. Building the plugin has nothing to do with your project or its PRO file and your project does not need to link with PostgreSQL libraries directly. You only need to link to PostgreSQL libraries directly if your code uses the PostgreSQL API directly, i.e. not through the Qt Sql libraries.
Open Qt Assistant and read the page Sql Database Drivers and particularly the bit about building the PostgreSQL driver on Mac, Linux, or Windows. Just substitute the correct PostgreSQL locations for your machine in the build commands. Once the plugin is built and installed QSqlDatabase::drivers() should show QPSQL.
Re: How to add a lib to a qt project
@ChrisW67:
The thing is that this plugin is probably already built ( qsqlpsql.dll exisits in his Qt installation ).
@WilliamSpiderWeb:
Have you tried with QApplication::addLibraryPath ? I think I've provided you with wrong info, plugins should be located in "sqldrivers" subdirectory, so it should look like:
QApplication::addLibraryPath("some path/plugins"), where "plugins" directory contains "sqldrivers" subdir (and you have psql dll file in this directory).
Re: How to add a lib to a qt project
I hope I understood it the right way.
My main() is looking like this now:
Code:
int main(int argc, char *argv[])
{
a.addLibraryPath("C:/Qt/2010.05/qt/plugins/sqldrivers");
MainWindow w;
w.show();
return a.exec();
}
But the available drivers are still the same.
The Question is, how should the software know which dll I need. It just knows where the dlls are located.
Re: How to add a lib to a qt project
No, I mean the "parent" directory for plugins:
Code:
QApplication::addLibraryPath("C:/Qt/2010.05/qt/plugins");
// without "sqldrivers"
Re: How to add a lib to a qt project
sorry, I tried first without "sqldrivers" and after it with "sqldrivers".
But I qoute the wrong codeline to my reply
Re: How to add a lib to a qt project
So it looks like you'll need to build it indeed - its quite easy, just follow the link ChrisW67 gave you in previous post (there is QPSQL section).
Re: How to add a lib to a qt project
I tried to build the psql plugin before,
but even there I need some support.
The doc says I have to use these commands
Code:
cd %QTDIR%\src\plugins\sqldrivers\psql
qmake "INCLUDEPATH+=C:\psql\include" "LIBS+=C:\psql\lib\ms\libpq.lib" psql.pro
nmake
when I type the second command (yes I used the right path for psql) I get no error but the psql.pro file is still empty.
when I type the nmake my windows tells me it cannot find nmake. And I can't find nmake in the qt directory either.
At this point I have to say, thanks for your effort and patience.