aknay
aknay

Reputation: 3425

How do I add QSerialPort Module into CMake?

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)

Upvotes: 5

Views: 7988

Answers (1)

aknay
aknay

Reputation: 3425

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)

Upvotes: 7

Related Questions