【发布时间】:2015-12-07 07:48:50
【问题描述】:
我想将 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()'
这是简单的 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)
【问题讨论】:
-
Command
qt5_use_modules(untitled1 Core)将您的可执行文件与 QTCore库链接,但您也使用SerivalPort库。请改用命令qt5_use_modules(untitled1 Core SerialPort)。
标签: c++ qt cmake serial-port