VTK/Tutorials/CMakeListsFileForQt4

From KitwarePublic
< VTK‎ | Tutorials
Jump to: navigation, search

CMakeLists.txt

project(application)
 
cmake_minimum_required(VERSION 2.8.0)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
 
# support for out-of-source build
INCLUDE_DIRECTORIES(
  ${CMAKE_CURRENT_BINARY_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}
)
 
set(SRC
    optionsDialog.h optionsDialog.cpp
    main.cpp
    mainwindow.h mainwindow.cpp
    application.qrc
)
 
set(MOC optionsDialog.h  mainwindow.h )
set(UI optionsDialog.ui mainwindow.ui)
set(RCC application.qrc )
 
qt4_wrap_cpp(MOC_GENERATED ${MOC})
qt4_wrap_ui(UI_GENERATED ${UI})
qt4_add_resources(RCC_GENERATED ${RCC})
 
source_group("Form Files" FILES ${UI} )
source_group("Resource Files" FILES ${RCC} )
source_group("Generated Files" FILES ${MOC_GENERATED}   ${UI_GENERATED}  ${RCC_GENERATED} )
 
add_executable(application
    ${SRC}
    ${MOC_GENERATED}   ${UI_GENERATED}  ${RCC_GENERATED} )
 
target_link_libraries(application  ${QT_LIBRARIES} QVTK ) # QVTK is for VTK 5.x. For VTK 6, use ${VTK_LIBRARIES} instead

Here is the descriptions for selected lines:

Make cmake to find VTK and Qt4 library and include UseVTK.cmake and UseQt4.cmake, where useful settings are defined.

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})

Tell cmake to include additional header directories for out-of-sorce build

# support for out-of-source build
INCLUDE_DIRECTORIES(
  ${CMAKE_CURRENT_BINARY_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}
)

Make cmake to define SRC for all source files, MOC for meta-compiling (class with Q_OBJECT), UI for form files,RCC for resource files.

set(SRC
    optionsDialog.h optionsDialog.cpp
    main.cpp
    mainwindow.h mainwindow.cpp
    application.qrc
)
 
set(MOC optionsDialog.h  mainwindow.h )
set(UI optionsDialog.ui mainwindow.ui)
set(RCC application.qrc )

Setting up for Qt preprocessing : moc.exe, ui.exe, rcc.exe

qt4_wrap_cpp(MOC_GENERATED ${MOC})
qt4_wrap_ui(UI_GENERATED ${UI})
qt4_add_resources(RCC_GENERATED ${RCC})

Assign filters. Optional, but useful.

source_group("Form Files" FILES ${UI} )
source_group("Resource Files" FILES ${RCC} )
source_group("Generated Files" FILES ${MOC_GENERATED}   ${UI_GENERATED}  ${RCC_GENERATED} )

Tell cmake the project "application"'s source files include all generated files and required libaries.

add_executable(application 
    ${SRC}
    ${MOC_GENERATED}   ${UI_GENERATED}  ${RCC_GENERATED} )
 
target_link_libraries(application  ${QT_LIBRARIES} QVTK ) # QVTK is for VTK 5.x. For VTK 6, use ${VTK_LIBRARIES} instead

In MSVC, WIN32 and $(QT_QTMAIN_LIBRARY} are required to remove dummy console window, as follows:

add_executable(application WIN32
    ${SRC}
    ${MOC_GENERATED}   ${UI_GENERATED}  ${RCC_GENERATED} )
 
target_link_libraries(application  ${QT_LIBRARIES} ${QT_QTMAIN_LIBRARY} QVTK ) # QVTK is for VTK 5.x. For VTK 6, use ${VTK_LIBRARIES} instead