Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error LNK2019 at compile a program whit Serial Port class

Error LNK2019 at compile a program whit Serial Port class

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Alan_641A Offline
    Alan_641A Offline
    Alan_641
    wrote on last edited by
    #1

    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 compile

    please, someone could help me and tell me what is the error and how compile it correctly?

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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>
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Alan_641A 1 Reply Last reply
      0
      • jsulmJ jsulm

        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>
        
        Alan_641A Offline
        Alan_641A Offline
        Alan_641
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Why are you creating a static QSerialPort object ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            For debug build you need Qt5SerialPortd.dll. Do you have it?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved