Search code examples
mysqlsqldatabaseqtlnk2019

qt creator error: LNK 2019: unresolved external symbol “__declspec(dllimport) public: void__thiscall


I would like to know why I am getting these errors.I am using Qt 5.0.2 and msvc2010 compiler. It runs normally when i delete the blah function. I'm not an expert programmer at all, please answer me as if i dont know anything, thank you!

Error: http://puu.sh/3m6Qr.png

My codes below:

.pro

QT       += core gui
QT       += widgets
QT       += network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = guangdong
TEMPLATE = app

SOURCES += main.cpp\
        login.cpp

HEADERS  += login.h

FORMS    += login.ui

login.cpp

#include "login.h"
#include "ui_login.h"
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QDebug>
#include <Query.h>
#include <QString>
#include <QtSql/QSqlQuery>
#include <QtNetwork/QNetworkInterface>

login::login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
    blah();
}

login::~login()
{
    delete ui;
}
void login::blah()
{
   // QSqlQuery query;
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("blah");
    db.setDatabaseName("blah");
    db.setUserName("blah");
    db.setPassword("blah");
    bool ok = db.open();

    if ( ok ) {
        ui->label->setText("databaseopen");
        db.close();
    }
    else
        ui->label->setText("Error opening");
}

main.cpp

#include "login.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    login w;
    w.show();

    return a.exec(&#41;;
}

EDIT: I added Qt += sql and #include but now i get this error. Error: http://puu.sh/3maq2.png


Solution

  • The error messages say the linker cannot find the external symbols defined in the header <QtSql/QSqlQuery>. You need to link against QtSql library/module: http://qt-project.org/doc/qt-5.0/qtsql/qtsql-index.html

    QT       += core gui
    QT       += widgets
    QT       += network
    QT       += sql
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = guangdong
    TEMPLATE = app
    
    SOURCES += main.cpp\
            login.cpp
    
    HEADERS  += login.h
    
    FORMS    += login.ui