Unsolved Error LNK2019 at compile a program whit Serial Port class
-
Hi! I im trying to compile a program whit serial port comunication but it show me the Error LNK2019 file not found:main.obj
this is the main.cpp code:
#include "mainwindow.h" #include <QApplication> #include <QTextStream> #include <QCoreApplication> #include <QFile> #include <QtCore> #include <QStringList> #include <QIODevice> #include <iostream> #include <QtSerialPort/QSerialPort> #include <QDebug> #include <Windows.h> #include <QElapsedTimer> QT_USE_NAMESPACE QSerialPort serial; int main(int argc, char *argv[]) { QElapsedTimer timer; QApplication a(argc, argv); QByteArray output; serial.setPortName ("COM3"); serial.open (QIODevice::ReadOnly); serial.setBaudRate (QSerialPort::Baud9600); serial.setDataBits (QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits (QSerialPort::OneStop); serial.setFlowControl (QSerialPort::NoFlowControl); while (!serial.isOpen()) serial.open(QIODevice::ReadOnly); while (true) { output = "a"; serial.write(output); serial.flush(); timer.start(); serial.waitForBytesWritten(100); } MainWindow w; w.show(); return a.exec(); } //your code here
this is the mainwindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_mano_abierto_clicked() { } //your code here
this is the mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_mano_abierto_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H //your code here
and this is the .pro file:
QT += core gui QT += serialport greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = brazo_gui TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui //your code here
and this is the error :
main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QSerialPort::QSerialPort(class QObject *)" (_imp??0QSerialPort@@QEAA@PEAVQObject@@@Z) referenced in function "void __cdecl `dynamic initializer for 'serial''(void)" (??__Eserial@@YAXXZ)
appear one of this for every line that include a funcion of the Serial Port class
when i delete it the program compile without errors
even if i only add the line QSerialPort serial; it not compileplease, someone could help me and tell me what is the error and how compile it correctly?
-
Does your Qt installation contain Qt5SerialPort.dll?
Is it a debug or release build?
Another possible problem: you're declaring serial outside of main, so it is initialized before main is executed. You should move it to main (after QApplication a(argc, argv), same for timer).
One note: you should include it like this (no need for QtSerialPort):#include <QSerialPort>
-
@jsulm
It's a debug project and yes, the installation contains the Qt5SerialPort.dll. I've already put the QSerialPort serial; code line inside the main after the QApplication a(argc, argv); line but when i compile it the same problem continues. -
Hi,
Why are you creating a static QSerialPort object ?
-
For debug build you need Qt5SerialPortd.dll. Do you have it?