how to link an app with "libqsqlpsql.so" by using QtCreator?

277 views Asked by At

Sorry! I know this is a stupid question, and may be duplicated with some one in somewhere, but I can NOT actually search it out(Neither google nor StackOverflow). Because I'm learnning Qt from scratch, so there are too many things to be grasped, I even can NOT finger out what should I search when the problem appeared.

I has made a sample app that want to connect a postgresql database. To make sure I has had appropriate db driver for postgresql, I check things as following at first.

auto drivers = QSqlDatabase::drivers();
foreach( const QString& drv, drivers )
    qDebug() << drv;

I saw that the output includes "QPSQL"/"QPSQL7", I think I can connect to the db.

But when I actually add the codes that does connecting, I encounter a linking error: "cannot find -lqsqlpsql". These are the codes can not be linked:

auto db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName( "localhost");
db.setUserName("postgres");
db.setPassword("123456");
db.setDatabaseName("qtall");
if ( db.open() ) {
    qDebug( "Opened!" );

I found a file named "libqsqlpsql.so" in "/home/myname/Qt5.12.3/5.12.3/gcc_64/plugins/sqldrivers", I guess that is the lib I need. Because the processing to build an app is in QtCreator, not by using gcc directly, I can NOT append any options to gcc. I guess there must be correct way to config Qt instaed, but I don't know.

This is my app.pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = job-manager
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

unix:!macx: LIBS += -lqsqlpsql

Any hints/tips are appeciated!

1

There are 1 answers

0
Leon On

I got it !

  1. append "sql" module to the first line of the .pro file as follow:

    QT += core gui sql

2.remove (comment out) the last line of same file:

unix:!macx: LIBS += -lqsqlpsql

In fact, the things I did is not insufficient, but too much more. Qt would find the shared lib libqsqlpsql.so automatically, as long as we append "sql" module to the first line "QT += ***".