使用 cmake 构建 Qt5 项目

build Qt5 project with cmake

我正在尝试使用 cmake 构建 Qt5 项目,这是我的 cmake 代码(VS2013 作为生成器):

cmake_minimum_required (VERSION 2.6 FATAL_ERROR)

project      (pcl-visualizer)
set(CMAKE_PREFIX_PATH "C:\Qt2013\5.7\msvc2013_64")
set(VTK_DIR "C:\Program Files\PCL 1.8.0\3rdParty\VTK\lib\cmake\vtk-7.0")
find_package(Qt5Widgets)
find_package (VTK REQUIRED)
find_package (PCL 1.8 REQUIRED)

include_directories (${PCL_INCLUDE_DIRS})
link_directories    (${PCL_LIBRARY_DIRS})
add_definitions     (${PCL_DEFINITIONS})

include_directories(${Qt5Widgets_INCLUDES})
add_definitions(${Qt5Widgets_DEFINITIONS})
set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
ADD_DEFINITIONS (${QT_DEFINITIONS})

set  (project_SOURCES main.cpp pclviewer.cpp)
set  (project_HEADERS pclviewer.h)
set  (project_FORMS   pclviewer.ui)
set  (VTK_LIBRARIES   vtkRendering vtkGraphics vtkHybrid QVTK)

ADD_DEFINITIONS (${QT_DEFINITIONS})

ADD_EXECUTABLE  (pcl_visualizer ${project_SOURCES}
                                ${project_FORMS_HEADERS}
                                ${project_HEADERS_MOC})

TARGET_LINK_LIBRARIES (pcl_visualizer ${QT_LIBRARIES} ${PCL_LIBRARIES} ${VTK_LIBRARIES})

并配置并生成。

-- Configuring done -- Generating done -- Build files have been written to: C:/ysong/pcl/doc/tutorials/content/sources/qt_visualizer/build 10:40:24: Le processus "C:\Program Files\CMake\bin\cmake.exe" s'est terminé normalement. 10:40:24: Impossible de trouver un Makefile. Vérifiez vos options de compilation. Erreur lors de la compilation/déploiement du projet pcl_visualizer (kit : Desktop Qt 5.6.1 MSVC2013 64bit2) When executing step "Make" 10:40:24: Temps écoulé : 00:01.

但是项目没有建好(没有ui_pclviewer.h)。我应该怎么做才能完整地构建我的项目。

一样,您的 CMake 文件首先需要清理。

cmake_minimum_required (VERSION 2.6 FATAL_ERROR)

2.6 太旧了,我很确定你真的不需要与旧版本兼容那么远。至少使用 2.8.11

set(CMAKE_PREFIX_PATH "C:\Qt2013\5.7\msvc2013_64")

避免在你的路径中使用“\”,你迟早会遇到麻烦。使用“/”代替,即使在 Windows:

set(CMAKE_PREFIX_PATH "C:/Qt2013/5.7/msvc2013_64")

您也可以设置 Qt5_DIR 而不是 CMAKE_PREFIX_PATH,就像您对 vtk 所做的那样。

不要通过以下方式一一查找每个 Qt 模块:

find_package(Qt5Widgets)
find_package(Qt5Gui)
find_package(Qt5Core)

一键添加:

find_package(Qt5 REQUIRED Widgets)

Widgets 就足够了,因为 Qt 的包文件处理依赖关系。添加 REQUIRED 以确保找到包。

使用 imported targetsAUTOMOCAUTOUIC,您的 cmake 项目应该如下所示:

cmake_minimum_required(VERSION 2.8.11)

project(pcl-visualizer)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(Qt5_DIR "C:/Qt2013/5.7/msvc2013_64/lib/cmake/Qt5")
set(VTK_DIR "C:/Program Files/PCL 1.8.0/3rdParty/VTK/lib/cmake/vtk-7.0")
find_package(Qt5 REQUIRED Widgets)
find_package (VTK REQUIRED)
find_package (PCL 1.8 REQUIRED)

include_directories (${PCL_INCLUDE_DIRS})
link_directories    (${PCL_LIBRARY_DIRS})
add_definitions     (${PCL_DEFINITIONS})

set  (project_SOURCES main.cpp pclviewer.cpp)
set  (project_HEADERS pclviewer.h)
set  (project_FORMS   pclviewer.ui)
set  (VTK_LIBRARIES   vtkRendering vtkGraphics vtkHybrid QVTK)

add_executable(pcl_visualizer ${project_SOURCES}
                              ${project_FORMS}
                              ${project_HEADERS})

target_link_libraries(pcl_visualizer Qt5::Widgets ${PCL_LIBRARIES} ${VTK_LIBRARIES})