如何将QSerialPort模块添加到CMake? [英] How do I add QSerialPort Module into CMake?

查看:95
本文介绍了如何将QSerialPort模块添加到CMake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将QSerialPort模块添加到CMake中.据我了解,我需要将QT + = serialport添加到* .pro中.我只想使用CMake.所以我尝试简单的CMake文件进行编译,但是它有错误.QtCore可以正常运行,因为qDebug可以毫无问题地显示出来.

I want to add QSerialPort Module into CMake. From my understanding, I need to add QT += serialport into *.pro. I only want to use CMake. So I try simple CMake file to compile but it has error. The QtCore is working as qDebug can display without any issue.

我得到的错误是:

undefined reference to `QSerialPort::QSerialPort(QObject*)'
undefined reference to `QSerialPort::~QSerialPort()'
undefined reference to `QSerialPort::~QSerialPort()'

这是简单的main.cpp文件.

This is the simple main.cpp file.

#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文件.

This is the simple CMake file.

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)

推荐答案

谢谢@ tsyvarev .您的建议解决了这个问题.仅出于其他人的参考,我将这些工作文件发回.

Thank you @tsyvarev. Your suggestion solved the problem. Just for the ref for other people, I post back those working files.

简单的main.cpp文件:

The simple main.cpp file:

#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文件:

The simple CMake file:

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)

这篇关于如何将QSerialPort模块添加到CMake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆