将CMakeLists QT4更新为QT5

时间:2018-06-07 10:42:21

标签: c++ qt cmake

我使用的是基于QT4的旧版CMakeLists

cmake_minimum_required(VERSION 2.8.0)
project(pol)

add_compile_options(-std=c++17)
find_package(catkin REQUIRED COMPONENTS qt_build roscpp sensor_msgs cv_bridge image_transport)
find_package(OpenCV REQUIRED)
include_directories(${catkin_INCLUDE_DIRS})

catkin_package()

rosbuild_prepare_qt4(QtCore QtGui) # Add the appropriate components to the component list here


file(GLOB QT_FORMS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ui/*.ui)
file(GLOB QT_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} resources/*.qrc)
file(GLOB_RECURSE QT_MOC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS include/pol/*.hpp)

QT4_ADD_RESOURCES(QT_RESOURCES_CPP ${QT_RESOURCES})
QT4_WRAP_UI(QT_FORMS_HPP ${QT_FORMS})
QT4_WRAP_CPP(QT_MOC_HPP ${QT_MOC})


file(GLOB_RECURSE QT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS src/*.cpp)


add_executable(pol ${QT_SOURCES} ${QT_RESOURCES_CPP} ${QT_FORMS_HPP} ${QT_MOC_HPP})
target_link_libraries(pol ${QT_LIBRARIES} ${catkin_LIBRARIES})
install(TARGETS pol RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

现在我正在尝试迁移到QT5的最新版本,所以我就这样开始了:

cmake_minimum_required(VERSION 3.1.0)

project(pol)

add_compile_options(-std=c++17)

find_package(catkin REQUIRED COMPONENTS qt_build roscpp sensor_msgs cv_bridge image_transport)
find_package(OpenCV REQUIRED)
include_directories(${catkin_INCLUDE_DIRS})

catkin_package()
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5 COMPONENTS Core Gui Widgets)

rosbuild_prepare_qt4(QtCore QtGui) # Add the appropriate components to the component list here

file(GLOB QT_FORMS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ui/*.ui)
file(GLOB QT_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} resources/*.qrc)
file(GLOB_RECURSE QT_MOC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS include/fmf_qt/*.hpp)

QT5_ADD_RESOURCES(QT_RESOURCES_CPP ${QT_RESOURCES})
QT5_WRAP_UI(QT_FORMS_HPP ${QT_FORMS})

include_directories(
    ${Qt5Core_INCLUDE_DIRS}
    ${Qt5Gui_INCLUDE_DIRS}
    ${Qt5Widgets_INCLUDE_DIRS}
    )

file(GLOB_RECURSE QT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS src/*.cpp)

add_executable(pol ${QT_SOURCES} ${QT_RESOURCES_CPP} ${QT_FORMS_HPP} ${QT_MOC_HPP})
target_link_libraries(pol Qt5::Widgets Qt5::Core ${QT_LIBRARIES} ${catkin_LIBRARIES})
install(TARGETS pol RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

现在我收到错误,我认为是由于我的新CMakeLists,例如

error: incomplete type ‘QImage’ used in nested name specifier
error: invalid use of incomplete type ‘class QImage’

该软件包是在ROS ubuntu 18.04 LTS下开发的,并使用gcc 8.1进行编译您是否知道我的新CMakeLists出了什么问题? 该软件包正在使用我的旧版CMakeLists

修改

我正确运行cmake

cmake ../src -DCMAKE_INSTALL_PREFIX=../install -DCATKIN_DEVEL_PREFIX=../devel

但是当我运行make

时,我收到了错误

2 个答案:

答案 0 :(得分:1)

您收到这些错误是因为当时正在编译的源文件正在使用QImage,但编译器只能看到前向声明而不是完整的类定义。

对于有问题的源文件,您需要添加(假设您的包含路径正确)...

#include <QImage>

以前,当使用Qt4时,QImage标头可能被其他Qt标头文件之一传递,这就是为什么没有错误。

答案 1 :(得分:1)

这是如何使用Qt与cmake:

cmake_minimum_required(VERSION 3.9)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

project(QCameraBug)

# required packages
find_package(Qt5 COMPONENTS Core Quick Multimedia REQUIRED)
find_package(OpenCV REQUIRED)

add_executable(${PROJECT_NAME} "main.cpp" "cannyfilter.cpp" "main.qrc")

target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)

target_link_libraries(${PROJECT_NAME}
    Qt5::Core
    Qt5::Quick
    Qt5::Multimedia
    ${OpenCV_LIBS}
)

这对我来说很有效。希望这有用