1
votes

i'm starting to program in visual Studio C++ (2013) and i'm using Qt(5.5.0) for user interface . i'm trying to connect to my sql server database (sql server 2012) .

it should be simple based on documentation . this is my code :

#include "qt_test1.h"
//#include "ui_qt_test1.h"
#include <qmessagebox.h>
#include <qfiledialog.h>
#include <QtGui>
#include <QtSql\qsql.h>
#include <QtSql\qsqldatabase.h>
#include <QtSql\qsqlerror.h>
#include <qdebug.h>

qt_test1::qt_test1(QWidget *parent)
: QMainWindow(parent)
{
    ui.setupUi(this);

}

qt_test1::~qt_test1()
{

}

// when button clicked try to connect to database
void qt_test1::on_btnShowHello_clicked()
{

    QSqlDatabase db = QSqlDatabase::addDatabase("mydb");
    db.setDatabaseName("C:\\Program Files\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\DATA\\qt_test_db");

    bool ok = db.open();
}

well when i try to debug i get this weird errorrs . its kind of missing dll thing which i dunno if its my code problem or qt installation error .

errors :

Error 2 error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase@@QEAA@XZ) referenced in function "private: void __cdecl qt_test1::on_btnShowHello_clicked(void)" (?on_btnShowHello_clicked@qt_test1@@AEAAXXZ) E:\Project\qt_test1\qt_test1\qt_test1.obj qt_test1

Error 3 error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __cdecl QSqlDatabase::open(void)" (__imp_?open@QSqlDatabase@@QEAA_NXZ) referenced in function "private: void __cdecl qt_test1::on_btnShowHello_clicked(void)" (?on_btnShowHello_clicked@qt_test1@@AEAAXXZ) E:\Project\qt_test1\qt_test1\qt_test1.obj qt_test1

Error 4 error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QSqlDatabase::setDatabaseName(class QString const &)" (__imp_?setDatabaseName@QSqlDatabase@@QEAAXAEBVQString@@@Z) referenced in function "private: void __cdecl qt_test1::on_btnShowHello_clicked(void)" (?on_btnShowHello_clicked@qt_test1@@AEAAXXZ) E:\Project\qt_test1\qt_test1\qt_test1.obj qt_test1

Error 5 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 "private: void __cdecl qt_test1::on_btnShowHello_clicked(void)" (?on_btnShowHello_clicked@qt_test1@@AEAAXXZ) E:\Project\qt_test1\qt_test1\qt_test1.obj qt_test1

Error 6 error LNK2001: unresolved external symbol "__declspec(dllimport) public: static char * QSqlDatabase::defaultConnection" (__imp_?defaultConnection@QSqlDatabase@@2PEADEA) E:\Project\qt_test1\qt_test1\qt_test1.obj qt_test1

Error 7 error LNK1120: 5 unresolved externals E:\Project\qt_test1\x64\Debug\qt_test1.exe qt_test1

2

2 Answers

2
votes

It's not your code, nor the Qt installation. You just don't understand how C/C++ compiles programs. I suggest you do some research into how the precompiler, compiler and linker work in order to fill in your knowledge gap.

In short, any time you get a "unresolved external symbol" error, this is an error generated by the linker and it means you've left some kind of library out of the project settings.

Qt comes with a whole range of modules, and these are organised into different libraries. The QSql module is just one of them.

If you have Qt's Visual Studio Add-in, this is easy to fix: just right-click on your project, select Qt Project Settings, and select SQL on the Qt Modules tab.

If you do not have the Add-in installed, you'll have to add the dependencies manually.

Right-click on your project, select Properties. Expand Configuration properties, then expand Linker and select Input. In the Additional Dependencies, add:

  1. Qt5Sqld.lib to the Debug configuration
  2. Qt5Sql.lib to the Release configuration
1
votes

Your project may miss QtSql.lib dependency. Somewhere in project configuration in VS you should have possibility to add linker additional dependencies - this library should be located in your Qt installation directory (Qt/lib). Hope it helps.