c++ - 由于未定义的 QSerialPortInfo 方法引用而无法编译

标签 c++ qt qt5.3

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

.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: %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();
}

这是一个非常简单的示例,说明如何查询系统中连接的设备,但是当我在 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

关于c++ - 由于未定义的 QSerialPortInfo 方法引用而无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222822/

相关文章:

java - 正确使用this->

c++ - 这里如何应用动态规划呢?

c++ - char和const char的区别

c++ - QThread - 使用 moveToThread 将类成员移动到线程

c++ - QWebView内存释放

c++ - 通过右值引用传递并使用 std::move() 返回?

Qt:如何找到路径最近的现有祖先,该路径本身可能存在也可能不存在

c++ - 工具提示中的 Qt WIdget

c++ - Qt 5.3 信号和槽、简单函数和 Lambda 表达式