PDA

View Full Version : How to add a lib to a qt project



WilliamSpiderWeb
26th February 2011, 19:58
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.


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:


cannot find -lpg
collect2: ld returned 1 exit status


I'm waiting for your help.
Thanks,
Alex

stampede
26th February 2011, 21:33
Where is this library located ? You need to point the linker to the library directory as well:

LIBS += -L"path/to/library/directory"

WilliamSpiderWeb
27th February 2011, 19:00
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.



LIBS += -lpg
LIBS += -L"D:\Qt-Projekte\SQL_BSP"


But I still got the same error messages.

stampede
27th February 2011, 19:08
Sorry, I guess you want the static linkage:


LIBS += "D:/Qt-Projekte/SQL_BSP/libpg.lib"

WilliamSpiderWeb
27th February 2011, 21:43
If I add the codeline you write and remove my two codelines I still get one errormessage.



collect2: ld returned 1 exit status


I've no idea what that error means.

stampede
27th February 2011, 22:07
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.

WilliamSpiderWeb
28th February 2011, 05:53
Yes, it's the only error message. In addition I get a warning.


Qmake does not support build directories below the source directory


This is my SQL_BSP.pro file.


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.

stampede
28th February 2011, 07:36
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 (http://doc.qt.nokia.com/latest/qtsql.html). There are many examples how to use it in Qt examples directory, and you have nice demo of sqlbrowser implemented with Qt as well.

WilliamSpiderWeb
1st March 2011, 08:06
Thank you for your help so far.
My SQL_BSP.pro now looks like this:


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:


#include <QtSql>
#include <QSqlError>
#include <QSqlDriver>
#include <QSqlDriverPlugin>

QSqlDatabase dbAlex = QSqlDatabase::addDatabase("QPSQL");
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";

QSqlError dbError = dbAlex.lastError();
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.

stampede
1st March 2011, 08:39
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 () (http://doc.qt.nokia.com/latest/qsqldatabase.html#drivers) method, it returns a list of all available drivers.

WilliamSpiderWeb
1st March 2011, 15:20
okay, I copied the "qsqlpsql4.dll" and "qsqlpsql.lib" to my project directory and added it in the *.pro file:



LIBS += -L"qsqlpsql4.lib"


In my *.cpp I asked for available drivers:



QSqlDatabase dbAlex;
QStringList slDrivers = dbAlex.drivers();


The Result ist slDrivers is:
QSQLITE
QODBC3
QODBC

But no PSQL or something similar.

stampede
1st March 2011, 18:21
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:

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) (http://doc.qt.nokia.com/latest/qcoreapplication.html#addLibraryPath) with path=<"directory with qt psql plugin"> after creating QApplication instance.

WilliamSpiderWeb
1st March 2011, 19:51
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.

ChrisW67
1st March 2011, 22:42
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 (http://doc.qt.nokia.com/latest/sql-driver.html) 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.

stampede
2nd March 2011, 08:14
@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).

WilliamSpiderWeb
2nd March 2011, 09:13
I hope I understood it the right way.
My main() is looking like this now:


int main(int argc, char *argv[])
{
QApplication a(argc, 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.

stampede
2nd March 2011, 09:36
No, I mean the "parent" directory for plugins:

QApplication::addLibraryPath("C:/Qt/2010.05/qt/plugins"); // without "sqldrivers"

WilliamSpiderWeb
2nd March 2011, 13:08
sorry, I tried first without "sqldrivers" and after it with "sqldrivers".
But I qoute the wrong codeline to my reply

stampede
2nd March 2011, 13:49
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).

WilliamSpiderWeb
2nd March 2011, 15:40
I tried to build the psql plugin before,
but even there I need some support.

The doc says I have to use these commands



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.

wysota
2nd March 2011, 19:20
I think the "problem" is a missing PostgreSQL dll, not the Qt driver. The driver also needs the dll.

ChrisW67
3rd March 2011, 05:13
Stampede spake saying:


The thing is that this plugin is probably already built ( qsqlpsql.dll exisits in his Qt installation ).

and WilliamSpiderWeb wrote earlier:

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

and gave us error messages from the GNU ld, i.e. the MingW compiler suite.

It took this to mean that his friend built the plugin with MSVC, and that WilliamSpiderWeb is using MingW. This plugin will not load into a MingW-built program for a variety of reasons. That's why I suggested building the plugin. Perhaps someone else can tell us if PostgreSQL would also have to built with MingW or if the downloadable binaries are sufficient?


To address one of the plugin build issues...

nmake is the make utility from the MSVC compiler suite. You are using the MingW compiler bundled in the Qt SDK. The equivalent command would be:


mingw32-make

stampede
3rd March 2011, 08:35
ChrisW67, you are probably right, I just assumed that the qt plugin was built during Qt installation.
I've just downloaded PostgreSQL from here : link (http://www.enterprisedb.com/products-services-training/pgbindownload)(Binaries from installer version 8.4.7-1 for Windows), and it took me 30 sec. to build the plugin (g++ 4.5.2):


cd C:\Qt\src\plugins\sqldrivers\psql
qmake "INCLUDEPATH+=C:\pgsql\include" "LIBS+=C:\pgsql\lib\libpq.lib" psql.pro
make

Just make sure that path in INCLUDEPATH and LIBS is correct.

WilliamSpiderWeb
3rd March 2011, 12:04
I'm not sure what I am doing wrong.
I started cmd, then moved to the directory "..\src\plugin\sqldrivers\psql".
The psql-folder was just created by me.
Now I typed the following command.


c:\qt\2010.05\qt\qmake\qmake.exe "INCLUDEPATH+=C:\psql9\include" "LIBS+=C:\psql9\lib\libpg.lib" psql.pro

The error message I get is


Cannot find file: psql.pro

When I create an empty textfile or copy an existing *.pro file to the directory "...sqldrivers\psql" I get the following error message


QMAKESPEC has not been set, so configuration cannot be deduced.
Error processing project file: psql.pro

wysota
4th March 2011, 10:40
Hmm... sorry for asking but what are you trying to do?

WilliamSpiderWeb
4th March 2011, 12:34
Am I on the completly wrong way?

I want my qt App communicate with a postgresql database.
Chris and stampede told me I have to build a psql plugin for qt.

That's what I'm actually trying to do.


cd C:\Qt\src\plugins\sqldrivers\psql
qmake "INCLUDEPATH+=C:\pgsql\include" "LIBS+=C:\pgsql\lib\libpq.lib" psql.pro
make


If I just type qmake, my pc cannot find the command. That's why I typed the whole path of qmake.exe.
And postgres is installed in C:\psql9 on my computer.
And as you can read in my last reply, it doesn't work on my computer.

wysota
4th March 2011, 12:45
I want my qt App communicate with a postgresql database.
Chris and stampede told me I have to build a psql plugin for qt.
And you intend to do it by compiling a project from a directory that doesn't contain any source (nor other project) files? You expect some magic to happen that your compiler knows how to build that plugin from an empty directory?


The psql-folder was just created by me.

You shouldn't be creating any directories. Qt sources should contain a psql directory inside src/plugins/imageformats and there should be a bunch of files there including a README file containing requirements and instructions for building the plugin.

WilliamSpiderWeb
4th March 2011, 14:03
I expected no kind of magic. But as you know that thread is posted in -Newbie- Forum. It's my first qt project and noone tells you what qmake and nmake are supposed to do. So I could be that qmake creates the psql.pro...

Anyway, now I know that it works in a different way.
I think my problem was I had installed the "qt sdk 2010.05" and the "qt 4.7.2". And in one folder the folder "...\qt\src\plugins\sqldrivers\psql" exists and in one not.

Now I've found the psql-project and the qmake-command seems to work.

But when I use mingw32-make.exe I get errors again.


C:/qt/2010.05/mingw/bin/mingw32-make -f Makefile.Debug all
mingw32-make[1]: Entering directory 'C:/Qt/2010.05/qt/src/plugins/sqldrivers/psql'
g++ -c -g -frtti -fexeptions -mthreads - Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_DLL -DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I "..\..\..\..\include\QtCore" -I"..\..\..\..\include\QtSql" -I"..\..\..\..\include" -I"c:\psql9\include" -I"..\..\..\..\include\ActiveQt" -I"tmp\moc\debug_shared" -I"..\..\..\..\mkspecs\win32-g++" -o tmp\obj\debug_shared\main.o main.cpp
Der Befehl "g++" ist entweder falsch geschrieben oder konnte nicht gefunden werden
mingw32-make[1]: *** [tmp/obj/debug_shared/main.o] Error 1
mingw32-make[1]: Leaving directory 'C:/Qt/2010.05/qt/src/plugins/sqldrivers/psql'
mingw32-make: *** [debug-all] Error 2


The German expression in the cmd-output means:


The command "g++" is wrong spelled or does not exist


I haven't chosen something with g++.
Please help me again.

wysota
4th March 2011, 16:00
I expected no kind of magic. But as you know that thread is posted in -Newbie- Forum.
If you look at the description of the forum you'll notice it says "Newbie to Qt", not "Newbie to programming".


It's my first qt project and noone tells you what qmake and nmake are supposed to do.
The docs for Qt and Visual Studio should tell you that. You can't expect to have zero knowledge about something and immediately use it to its full potential. Maybe your first Qt project should be something simpler than accessing databases.


But when I use mingw32-make.exe I get errors again.


The German expression in the cmd-output means:


I haven't chosen something with g++.
Please help me again.

Please learn to use a compiler.

WilliamSpiderWeb
4th March 2011, 23:22
That's not my first programming project at all.
Your post was not that helpful.
I don't know why you are going to do verbal attacks against me.

wysota
4th March 2011, 23:46
I'm not doing any verbal attacks against you. I'm just irritated with "I'm a newbie, be nice and fuzzy and do everything for me" attitude. If you use a tool, any tool, learn what it does before you use it for the first time. Otherwise one day you might be unpleasantly suprised when some tool destroys something important for you.

One of the first sentences in qmake docs says:

qmake generates a Makefile based on the information in a project file.
This gives you the following information:
1. qmake is a tool
2. it needs a project file
3. it generates something called a "Makefile"

And then there is two sentences that you have written:

It's my first qt project and noone tells you what qmake and nmake are supposed to do. So I could be that qmake creates the psql.pro...

One sentence from the docs can tell you so many things noone had told you before. Maybe it's time to get a bit more familiar with manuals? It's really nothing personal, I have nothing against you just please don't excuse yourself by saying that nobody told you what some tool did.

ChrisW67
7th March 2011, 00:01
If I just type qmake, my pc cannot find the command. That's why I typed the whole path of qmake.exe.
And postgres is installed in C:\psql9 on my computer.
And as you can read in my last reply, it doesn't work on my computer.

Here it is blow by blow:

Open your Windows Start menu
Locate the "Qt SDK by Nokia (2010.05)" folder
Open it and select "Qt Command Prompt"
In the new command prompt everything is correctly set to find the Qt SDK tools (like qmake and mingw32-make). Test by issuing a "qmake -v" command. You should get a Qmake and Qt version number. If you don't get the version message then stop, you won't get any further.
Locate the Qt source code in the installed Qt SDK. The default location would be something like "C:\qt\2010.05\qt\src" but you will have to use your knowledge of your system to find it. If you cannot find the sources then stop, you won't get any further.
In the command prompt change directory to the psql driver directory in the Qt source. The command prompt environment has a variable that points to the Qt directory, QTDIR, so this is the quickest way to get there:


CD %QTDIR%
CD src\plugins\sqldrivers\psql
qmake "INCLUDEPATH+=C:\pgsql\include" "LIBS+=C:\pgsql\lib\libpq.lib" psql.pro
mingw32-make

WilliamSpiderWeb
13th March 2011, 23:34
Hey Chris,
thanks for helping me again.

I followed your detailed instructions.
The differences to my last try is, this time I used "Qt command prompt". Last time it was "Windows command prompt". And there was a blank in the installation path of postgresql. I elliminated the blank for plugin building now.

There were no error messages while building the plugin now.

But I haven't understood what to do now to use the plugin.

WilliamSpiderWeb
14th March 2011, 22:50
Okay,
Here in Qt Centre I found this wiki article:
http://www.qtcentre.org/wiki/index.php?title=Building_the_QPSQL_plugin_on_Windo ws_using_MinGW

When I compare what I've done with this article, I find the files "qsqlpsql4.dll" and "qsqlpsqld4.dll" in the folder "%QTDIR%\plugins\sqldrivers".

If I load the "qsqlpsql4.dll" with the dependency walker I get the information that there are a few files missing (libpq.dll, mingwm10.dll, libgcc_s_dw2-1.dll, qtcore4.dll, qtsql4.dll).

Tomorrow I'll try to update my enviromental variable paths.
Please tell me if I'm completly wrong.

Greetings

wysota
14th March 2011, 23:12
Lot to learn you still have, young padawan. However on the right path you are.

ChrisW67
15th March 2011, 00:01
Okay,
If I load the "qsqlpsql4.dll" with the dependency walker I get the information that there are a few files missing (libpq.dll, mingwm10.dll, libgcc_s_dw2-1.dll, qtcore4.dll, qtsql4.dll).

When you started Dependency Walker it was given the default Windows environment PATH, which does not include the Qt bin directories that contain all of the Qt and MingW files listed there. The default environment PATH also does not contain the PostgreSQL binary directory for the PostgreSQL library. Consequently, the Dependency Walker could not load any of these DLLs. If you had launched the Dependency Walker from the "Qt Command Prompt" most of the libraries, except the Postgres ones, would have been found.

You should add the PostgreSQL bin directory to your system PATH. If you develop your program in an environment with the Qt paths set properly such as the Qt Command Prompt or Qt Creator then you should not need to add QT or MingW stuff to the system path. When you deploy you will need to ship the MingW, PostgreSQL and Qt libraries with your application.

WilliamSpiderWeb
15th March 2011, 20:45
Now I added the following paths to the enviroment variable path


C:\psql9\bin
C:\qt\2010.05\bin
C:\qt\2010.05\qt\bin
C:\qt\2010.05\mingw\bin


But then there are more and more dlls the dependency walker cannot found or with other errors


Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.

WilliamSpiderWeb
18th March 2011, 23:26
Just to inform other Newbies who want to connect to PostgreSQL databases as well...

My last-ditch attempt was, just don't care about what the results of the dependency walker are and copied ALL dlls of %postgresdir%\bin.

Now I can load the QPSQL Driver and connect to my postgresql database.

At this point I want to thank the experts of this forum again for trying to help me.


Greetz, wsw