2015-12-07 23 views
2

我想將QSerialPort模塊添加到CMake中。根據我的理解,我需要將QT + = serialport添加到* .pro中。我只想使用CMake。所以我嘗試簡單的CMake文件來編譯,但它有錯誤。 QtCore正在工作,因爲qDebug可以顯示沒有任何問題。如何將QSerialPort模塊添加到CMake中?

我得到的錯誤是:

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) 
+3

命令'qt5_use_modules(未命名睿)'鏈路與QT'Core'庫中的可執行文件,但使用也有「SerivalPort」庫。改爲使用命令'qt5_use_modules(untitled1 Core SerialPort)'。 – Tsyvarev

回答

2

謝謝你@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) 
相關問題