尝试添加数据库时出现 Qt PostgreSQL 链接器错误

Qt PostgreSQL linker error when trying to add database

提问人:goodle06 提问时间:7/14/2020 更新时间:7/14/2020 访问量:86

问:

我正在尝试设置与 postgre 数据库的连接,但出现链接器错误。

.pro 文件:

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle
CONFIG += sql

DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

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


unix|win32: LIBS += -L$$PWD/../../PostgreSQL/12/lib/ -llibpq

INCLUDEPATH += $$PWD/../../PostgreSQL/12/include
DEPENDPATH += $$PWD/../../PostgreSQL/12/include

win32:!win32-g++: PRE_TARGETDEPS += $$PWD/../../PostgreSQL/12/lib/libpq.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$PWD/../../PostgreSQL/12/lib/liblibpq.a

.cpp 文件:

#include <QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSql>
#include <QtSql/QSqlDriver>
#include <QTextStream>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase mDB=QSqlDatabase::addDatabase("QPSQL");


    return a.exec();
}

只是。我什至还没有尝试连接。 错误包括:

main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QEAA@XZ) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QSqlDatabase __cdecl QSqlDatabase::addDatabase(class QString const &,class QString const &)" (__imp_?addDatabase@QSqlDatabase@@SA?AV1@AEBVQString@@0@Z) referenced in function main
main.obj:-1: error: LNK2001: unresolved external symbol "__declspec(dllimport) public: static char const * const QSqlDatabase::defaultConnection" (__imp_?defaultConnection@QSqlDatabase@@2PEBDEB)

Postge 是 Windows 的 64 位,为此应用程序选择的编译器也是 64 MSVC2019 64 位。请帮忙。

PostgreSQL Qt 链接器错误

评论


答:

0赞 goodle06 7/14/2020 #1

问题出在.pro文件中。它应该是:

QT += sql

而不是:

CONFIG += sql