How do I add QSerialPort Module into CMake?

自闭症网瘾萝莉.ら 提交于 2021-01-27 11:57:59

问题


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.

The error I am getting is:

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

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;
}

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)

回答1:


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

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;
}

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)


来源:https://stackoverflow.com/questions/34128686/how-do-i-add-qserialport-module-into-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!