我想将 QSerialPort 模块添加到 CMake 中。根据我的理解,我需要将 QT += serialport 添加到 *.pro 中。我只想使用 CMake。所以我尝试编译简单的 CMake 文件,但它有错误。QtCore 正在工作,因为 qDebug 可以毫无问题地显示。
我得到的错误是:
undefined reference to `QSerialPort::QSerialPort(QObject*)'
undefined reference to `QSerialPort::~QSerialPort()'
undefined reference to `QSerialPort::~QSerialPort()'
Run Code Online (Sandbox Code Playgroud)
这是简单的 main.cpp 文件。
#include <iostream>
#include <QObject>
#include <QDebug>
#include <QCoreApplication>
#include <QtSerialPort/QSerialPort>
using namespace std;
int main() {
QSerialPort serialPort; //this line gives error
qDebug()<<"Hello Qt"; //this line is working as normal
cout << "Hello, World!" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是简单的 CMake 文件。
cmake_minimum_required(VERSION 3.3)
project(untitled1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} …Run Code Online (Sandbox Code Playgroud)