【问题标题】:How do I add QSerialPort Module into CMake?如何将 QSerialPort 模块添加到 CMake 中?
【发布时间】: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) 将您的可执行文件与 QT Core 库链接,但您也使用 SerivalPort 库。请改用命令qt5_use_modules(untitled1 Core SerialPort)

标签: c++ qt cmake serial-port


【解决方案1】:

谢谢@tsyvarev。你的建议解决了问题。只是为了其他人的参考,我回发那些工作文件。

简单的 main.cpp 文件:

#include <iostream>
#include <QObject>
#include <QDebug>
#include <QCoreApplication>
#include <QtSerialPort>

using namespace std;

int main() {
    QSerialPort serialPort;
    serialPort.setPortName("ttyACM1");
    qDebug()<<"Hello Qt";
    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 REQUIRED)

set(SOURCE_FILES main.cpp)
add_executable(untitled1 ${SOURCE_FILES})
qt5_use_modules(untitled1 Core SerialPort)

【讨论】:

    猜你喜欢
    • 2023-01-24
    • 1970-01-01
    • 2012-09-09
    • 2016-05-27
    • 1970-01-01
    • 2023-03-09
    • 2018-01-05
    • 2013-11-01
    • 2011-09-05
    相关资源
    最近更新 更多