如何将QSerialPort模块添加到CMake中?

时间:2015-12-07 07:48:50

标签: c++ qt cmake serial-port

我想将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)

1 个答案:

答案 0 :(得分:4)

谢谢@ 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)