将类添加到基本Qt GUI应用程序时出现Qt链接器错误
Qt linker error when adding class to basic Qt GUI application
我已经挣扎了一段时间,无法解决这个问题。我在我的基本QtGUI应用程序中添加了一个类,但我得到了链接器错误。我不知道是什么原因造成的,希望得到反馈。
链接器错误:
main.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall Serial::start(enum QSerialPort::BaudRate)" (?start@Serial@@QAEXW4BaudRate@QSerialPort@@@Z) referenced in function _main
main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Serial::Serial(class QObject *)" (??0Serial@@QAE@PAVQObject@@@Z) referenced in function _main
debugTest.exe:-1: error: LNK1120: 2 unresolved externals
这是项目文件:
#-------------------------------------------------
#
# Project created by QtCreator 2013-11-18T10:08:32
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Test
TEMPLATE = app
SOURCES += main.cpp
mainwindow.cpp
serial.cpp
HEADERS += mainwindow.h
serial.h
FORMS += mainwindow.ui
serial.h文件:
#ifndef SERIAL_H
#define SERIAL_H
#include <QObject>
#include <QtSerialPort/QSerialPort>
class Serial : public QObject
{
Q_OBJECT
public:
explicit Serial(QObject *parent = 0);
void start(QSerialPort::BaudRate baudRate);
bool Serial::closePort();
void Serial::readData();
signals:
public slots:
};
#endif // SERIAL_H
serial.cpp文件:
#include "serial.h"
#include <QObject>
#include <QtSerialPort/QSerialPort>
Serial::Serial(QObject *parent) :
QObject(parent)
{
serial = new QSerialPort(this);
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
}
bool Serial::start(QSerialPort::BaudRate baudRate)
{
if(serial.open(QIODevice::ReadWrite))
{
//TODO: Connection OK is green in GUI
QDebug << "Port opened successfully" << std::endl;
return true;
}
else
{
//Open failed.
QDebug << "Port failed to open" << std::endl;
//TODO: Show error window in GUI
return false;
}
if(
serial.setBaudRate(baudRate) &&
serial.setDataBits(QSerialPort::Data8) &&
serial.setParity(QSerialPort::NoParity) &&
serial.setStopBits(QSerialPort::OneStop) &&
serial.setFlowControl(QSerialPort::NoFlowControl)
)
{
//Connection ok.
QDebug << "Parameters set OK" << std::endl;
}
else
{
QDebug << "Parameters set FAILED" << std::endl;
Serial::closePort();
}
}
bool Serial::closePort()
{
serial.close();
//TODO: Connection OK is red in GUI
}
void Serial::readData()
{
this->readDataArray = this->serial.readAll();
}
如前所述,由于QtSarialPort是一个附加模块,您需要将其明确添加到您的项目中:
QT += serialport
相关文章:
- Qt creator 4.11,在应用程序输出面板中创建一个链接
- Libharu 解释 QT c++ 中的链接
- Qt5 C++将新窗口与Qt设计器链接
- 确定Qt库链接
- Qt & Firebase C++ SDK 在 iOS 上的链接问题
- 在Qt中链接具有非标准名称的lib文件
- CMake Qt建立在Windows链接器问题之上
- libGL确实存在,但链接器找不到它 - Qt 4.8.7,CentOS 7.5
- 静态链接MSVC库,动态链接Qt
- 将Qt应用程序与CMake和Visual Studio链接起来
- 静态Qt链接版本,Qt 5.2,QT中的未知模块:快速qml
- Qt链接问题
- CGAL Qt 链接器错误
- qt链接器错误,从另一个线程发出信号
- 将类添加到基本Qt GUI应用程序时出现Qt链接器错误
- Ubuntu Qt链接提升日志
- MyClass::元对象VS2005的Qt链接问题
- Qt链接器错误窗口
- Qt:链接我的模型数据与QListView
- Q_OBJECT相关的qt链接错误