我在使用 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 源文件夹下,我相信我已经包含了所有必要的引用。
我很困惑,不知道我错过了什么。
最佳答案
关于c++ - 由于未定义的 QSerialPortInfo 方法引用而无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222822/