我希望将QSerialPort模块添加到CMake中。据我所知,我需要在*.pro文件中添加QT += serialport。我只想使用CMake。因此,我尝试使用简单的CMake文件进行编译,但是出错了。QtCore工作正常,因为qDebug可以正常显示。
我得到的错误信息是:
这是一个简单的CMake文件。
我得到的错误信息是:
undefined reference to `QSerialPort::QSerialPort(QObject*)'
undefined reference to `QSerialPort::~QSerialPort()'
undefined reference to `QSerialPort::~QSerialPort()'
这是一个简单的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;
}
这是一个简单的CMake文件。
cmake_minimum_required(VERSION 3.3)
project(untitled1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(Qt5Core COMPONENTS Qt5SerialPort REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(untitled1 ${SOURCE_FILES})
qt5_use_modules(untitled1 Core)
qt5_use_modules(untitled1 Core)
将你的可执行文件链接到QTCore
库,但是你也使用了SerialPort
库。请改用命令qt5_use_modules(untitled1 Core SerialPort)
。 - Tsyvarev