Lucien Song Lucien Song - 21 days ago 8
C++ Question

build Qt5 project with cmake

I am trying to build a Qt5 project with cmake, here is my cmake code(VS2013 as generator):

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})


And it configured and generated.


-- 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.


But the project is not built(no ui_pclviewer.h). What should I do to build completely my project.

Answer

Like in this post, your CMake file first need to be cleaned. You can take a look at SO documentation for a minimal example of CMakeLists.txt using Qt.

cmake_minimum_required (VERSION 2.6 FATAL_ERROR)

2.6 is too old and I'm pretty sure you don't really need to be that far compatible with old versions. Use 2.8.11 at the very least.

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

Avoid "\" in your paths, sooner or later you will have trouble with that. Use "/" instead, even on Windows:

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

You may also set Qt5_DIR instead of CMAKE_PREFIX_PATH, like you did for vtk.

Don't find each Qt module one by one with:

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

Add them all with one call:

find_package(Qt5 REQUIRED Widgets)

Widgets is enough since Qt's package files handle dependencies. Add REQUIRED to ensure packages are found.

Using imported targets, AUTOMOC and AUTOUIC, your cmake project should look like this:

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_HEADERS}
                              ${project_HEADERS_MOC})

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