用cmake构建Qt5项目

时间:2016-11-16 09:49:20

标签: c++ qt cmake qt5 point-cloud-library

我正在尝试用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})

它已配置并生成。

  

- 配置完成    - 生成完成    - 构建文件已写入:C:/ ysong / pcl / doc / tutorials / content / sources / qt_visualizer / build   10:40:24:Le processus“C:\ Program Files \ CMake \ bin \ cmake.exe”s'est   终点法则。 10:40:24:不可能出现问题。   Vérifiezvosoptions de compilation。 Erreur lors de la   compilation /déploiementduprojet pcl_visualizer(kit:Desktop Qt   5.6.1 MSVC2013 64bit2)执行步骤“Make”10:40:24:Tempsécoulé:00:01。

但是项目没有建成(没有ui_pclviewer.h)。我该怎么做才能完全构建我的项目。

1 个答案:

答案 0 :(得分:1)

this post一样,您的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})