Search code examples
c++qtqt5.3

Cannot compile due to undefined QSerialPortInfo method references


I'm facing a odd problem with QSerialPortInfo on Qt Creator. To illustrate the problem as simple as possible lets look at this small code snippet.

.pro

I have included CONFIG += serialport of course.

main.cpp

#include <QGuiApplication>
#include <QTimer>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

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

    QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
    QList<QSerialPortInfo>::iterator i;
    for(i = ports.begin(); i != ports.end(); i++)
    {
    printf("Port:\n");
    printf(" - Name: %s\n", (*i).portName().toStdString().data());
    printf(" - Description: %s\n", (*i).description().toStdString().data());
    printf(" - Manufacturer: %s\n", (*i).manufacturer().toStdString().data());
    printf(" - Serial number: %s\n", (*i).serialNumber().toStdString().data());
    printf(" - System location: %s\n", (*i).systemLocation().toStdString().data());
    printf("\n");

    QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit()));
    return a.exec();
}

This is a quite simple example of how to query the system for devices connected to it, however when I compile this on QtCreator, make reports all methods as undefined.

undefined reference to `QSerialPortInfo::availablePorts()'
undefined reference to `QSerialPortInfo::portName() const'
undefined reference to `QSerialPortInfo::description() const'
undefined reference to `QSerialPortInfo::manufacturer() const'
undefined reference to `QSerialPortInfo::serialNumber() const'
undefined reference to `QSerialPortInfo::systemLocation()'

Also tried #include <QtSerialPort/qserialportinfo.h> to be sure, yet no changes.

I also took a look and the QSerialPort files exist under the Qt source folder and I believe I've included all necessary references to it.

I'm puzzled and don't know what I'm missing.


Solution

  • Try this:

    QT += serialport
    

    instead of this:

    CONFIG += serialport
    

    Link: http://doc.qt.io/qt-5/qserialportinfo.html