Results 1 to 20 of 38

Thread: How to add a lib to a qt project

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    Qt Code:
    1. QT += core gui
    2.  
    3. TARGET = SQL_BSP
    4. TEMPLATE = app
    5.  
    6. LIBS += -lpg
    7.  
    8. SOURCES += main.cpp\
    9. mainwindow.cpp \
    10. postgresqldb.cpp
    11.  
    12. HEADERS += mainwindow.h \
    13. libpq-fe.h \
    14. postgres_ext.h \
    15. postgresqldb.h
    16.  
    17. FORMS += mainwindow.ui
    18.  
    19. OTHER_FILES += \
    20. libpq.lib
    To copy to clipboard, switch view to plain text mode 

    When I start compiling my project I get the following error message:
    Qt Code:
    1. cannot find -lpg
    2. collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 

    I'm waiting for your help.
    Thanks,
    Alex

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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:
    Qt Code:
    1. LIBS += -L"path/to/library/directory"
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

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

    Qt Code:
    1. LIBS += -lpg
    2. LIBS += -L"D:\Qt-Projekte\SQL_BSP"
    To copy to clipboard, switch view to plain text mode 

    But I still got the same error messages.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to add a lib to a qt project

    Sorry, I guess you want the static linkage:
    Qt Code:
    1. LIBS += "D:/Qt-Projekte/SQL_BSP/libpg.lib"
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

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

    Qt Code:
    1. collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 

    I've no idea what that error means.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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.
    Last edited by stampede; 27th February 2011 at 22:09. Reason: updated contents

  7. #7
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add a lib to a qt project

    Yes, it's the only error message. In addition I get a warning.
    Qt Code:
    1. Qmake does not support build directories below the source directory
    To copy to clipboard, switch view to plain text mode 

    This is my SQL_BSP.pro file.
    Qt Code:
    1. QT += core gui
    2.  
    3. TARGET = SQL_BSP
    4. TEMPLATE = app
    5.  
    6. LIBS += -L"D:/Qt-Projekte/SQL_BSP/libpg.lib"
    7.  
    8. SOURCES += main.cpp\
    9. mainwindow.cpp \
    10. postgresqldb.cpp
    11.  
    12. HEADERS += mainwindow.h \
    13. libpq-fe.h \
    14. postgres_ext.h \
    15. postgresqldb.h
    16.  
    17. FORMS += mainwindow.ui
    18.  
    19. OTHER_FILES += \
    20. libpq.lib
    To copy to clipboard, switch view to plain text mode 

    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.

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to add a lib to a qt project

    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.

  9. #9
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. QT += core gui
    2. QT += sql
    3.  
    4. TARGET = SQL_BSP
    5. TEMPLATE = app
    6.  
    7. SOURCES += main.cpp\
    8. mainwindow.cpp
    9.  
    10. HEADERS += mainwindow.h
    11.  
    12. FORMS += mainwindow.ui
    13.  
    14. OTHER_FILES += \
    15. libpq.lib
    To copy to clipboard, switch view to plain text mode 

    I tried to get access to the sql db like this:
    Qt Code:
    1. #include <QtSql>
    2. #include <QSqlError>
    3. #include <QSqlDriver>
    4. #include <QSqlDriverPlugin>
    5.  
    6. QSqlDatabase dbAlex = QSqlDatabase::addDatabase("QPSQL");
    7. dbAlex.setHostName("localhost");
    8. dbAlex.setPort(5433);
    9. dbAlex.setUserName("postgres");
    10. dbAlex.setPassword("***");
    11. dbAlex.setDatabaseName("QT");
    12. dbAlex.open("postgres", "***");
    13.  
    14. if (dbAlex.isOpen() == true)
    15. m_sConnState = "true";
    16. else
    17. m_sConnState = "false";
    18.  
    19. QSqlError dbError = dbAlex.lastError();
    20. m_sConnState = dbError.text();
    21. ui->lineEdit->setText(m_sConnState);
    To copy to clipboard, switch view to plain text mode 

    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.

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

  11. #11
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. LIBS += -L"qsqlpsql4.lib"
    To copy to clipboard, switch view to plain text mode 

    In my *.cpp I asked for available drivers:

    Qt Code:
    1. QSqlDatabase dbAlex;
    2. QStringList slDrivers = dbAlex.drivers();
    To copy to clipboard, switch view to plain text mode 

    The Result ist slDrivers is:
    QSQLITE
    QODBC3
    QODBC

    But no PSQL or something similar.

Similar Threads

  1. Qt project management - bigger project
    By Peppy in forum Qt Programming
    Replies: 11
    Last Post: 24th December 2010, 13:50
  2. How to add a gif to a Qt Project?
    By Bong.Da.City in forum Newbie
    Replies: 5
    Last Post: 6th September 2010, 21:51
  3. new project
    By rk0747 in forum Qt Programming
    Replies: 5
    Last Post: 17th February 2010, 08:53
  4. Replies: 1
    Last Post: 3rd December 2009, 23:34
  5. Project generation on linux from QT Project
    By darpan in forum Qt Programming
    Replies: 6
    Last Post: 11th December 2006, 09:43

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.