由于未定义QSerialPortInfo方法引用,无法编译

Cannot compile due to undefined QSerialPortInfo method references

本文关键字:编译 引用 方法 未定义 QSerialPortInfo      更新时间:2023-10-16

我在Qt Creator上遇到QSerialPortInfo的一个奇怪问题。为了尽可能简单地说明这个问题,让我们看看这个小代码片段。

.pro

我当然包括了CONFIG += serialport

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: %sn", (*i).portName().toStdString().data());
    printf(" - Description: %sn", (*i).description().toStdString().data());
    printf(" - Manufacturer: %sn", (*i).manufacturer().toStdString().data());
    printf(" - Serial number: %sn", (*i).serialNumber().toStdString().data());
    printf(" - System location: %sn", (*i).systemLocation().toStdString().data());
    printf("n");
    QTimer::singleShot(0, QCoreApplication::instance(), SLOT(quit()));
    return a.exec();
}

这是一个非常简单的例子,说明了如何查询系统中连接到它的设备,但是当我在QtCreator上编译它时,make会将所有方法报告为未定义。

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()'

还尝试了#include <QtSerialPort/qserialportinfo.h>以确定,但没有更改。

我还看了一下,QSerialPort文件存在于Qt源文件夹下,我相信我已经包含了对它的所有必要引用。

我很困惑,不知道我错过了什么。

试试这个:

QT += serialport

而不是这个:

CONFIG += serialport

链接:http://doc.qt.io/qt-5/qserialportinfo.html

相关文章: