VTK/Tutorials/CMakeListsFileForQt4: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
(Created page with '== CMakeLists.txt == <source lang=cmake> project(application) cmake_minimum_required(VERSION 2.8.0) find_package(VTK) include(${VTK_USE_FILE}) find_package(Qt4 REQUIRED) inclu…')
 
m (: VTK 5 vs 6)
 
(8 intermediate revisions by 2 users not shown)
Line 5: Line 5:
cmake_minimum_required(VERSION 2.8.0)
cmake_minimum_required(VERSION 2.8.0)


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


Line 36: Line 36:
source_group("Generated Files" FILES ${MOC_GENERATED}  ${UI_GENERATED}  ${RCC_GENERATED} )
source_group("Generated Files" FILES ${MOC_GENERATED}  ${UI_GENERATED}  ${RCC_GENERATED} )


add_executable(application WIN32
add_executable(application
     ${SRC}
     ${SRC}
     ${MOC_GENERATED}  ${UI_GENERATED}  ${RCC_GENERATED} )
     ${MOC_GENERATED}  ${UI_GENERATED}  ${RCC_GENERATED} )


target_link_libraries(application  ${QT_LIBRARIES} ${QT_QTMAIN_LIBRARY} QVTK vtkHybrid)
target_link_libraries(application  ${QT_LIBRARIES} QVTK ) # QVTK is for VTK 5.x. For VTK 6, use ${VTK_LIBRARIES} instead


</source>
</source>


== ==
Here is the descriptions for selected lines:


Here is a line by line break down of what is going on:
Make cmake to find VTK and Qt4 library and include UseVTK.cmake and UseQt4.cmake,
where useful settings are defined.
<source lang=cmake>
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})


This will prevent annoying cmake warnings about "you must specifiy a minimum required version".
find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
</source>
 
Tell cmake to include additional header directories for out-of-sorce build
<source lang=cmake>
<source lang=cmake>
cmake_minimum_required(VERSION 2.6)
# support for out-of-source build
INCLUDE_DIRECTORIES(
  ${CMAKE_CURRENT_BINARY_DIR}
  ${CMAKE_CURRENT_SOURCE_DIR}
)
</source>
</source>


Name the project.
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.
 
<source lang=cmake>
<source lang=cmake>
PROJECT(Test)
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 )
</source>
 
Setting up for Qt preprocessing : moc.exe, ui.exe, rcc.exe
<source lang=cmake>
qt4_wrap_cpp(MOC_GENERATED ${MOC})
qt4_wrap_ui(UI_GENERATED ${UI})
qt4_add_resources(RCC_GENERATED ${RCC})
</source>
</source>


Tell cmake that this project needs to use VTK.
Assign filters. Optional, but useful.
<source lang=cmake>
<source lang=cmake>
FIND_PACKAGE(VTK REQUIRED)
source_group("Form Files" FILES ${UI} )
INCLUDE(${VTK_USE_FILE})
source_group("Resource Files" FILES ${RCC} )
source_group("Generated Files" FILES ${MOC_GENERATED}  ${UI_GENERATED}  ${RCC_GENERATED} )
</source>
</source>


Tell cmake we want to compile Test.cpp into a binary called Test.
Tell cmake the project "application"'s source files include all generated files
and required libaries.
 
<source lang=cmake>
<source lang=cmake>
ADD_EXECUTABLE(Test Test.cpp)
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
</source>
</source>


Tell cmake we need to link Test against vtkHybrid. This is the "catch all" VTK library. If you don't know what to link to, most of the time vtkHybrid will have what you need.
In MSVC, WIN32 and $(QT_QTMAIN_LIBRARY} are required to remove dummy console window, as follows:
 
<source lang=cmake>
<source lang=cmake>
TARGET_LINK_LIBRARIES(Test vtkHybrid)
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
</source>
</source>

Latest revision as of 14:33, 21 August 2013

CMakeLists.txt

<source lang=cmake> 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})

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

</source>

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. <source lang=cmake> find_package(VTK REQUIRED) include(${VTK_USE_FILE})

find_package(Qt4 REQUIRED) include(${QT_USE_FILE}) </source>

Tell cmake to include additional header directories for out-of-sorce build <source lang=cmake>

  1. support for out-of-source build

INCLUDE_DIRECTORIES(

 ${CMAKE_CURRENT_BINARY_DIR}
 ${CMAKE_CURRENT_SOURCE_DIR}

) </source>

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.

<source lang=cmake> 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 ) </source>

Setting up for Qt preprocessing : moc.exe, ui.exe, rcc.exe <source lang=cmake> qt4_wrap_cpp(MOC_GENERATED ${MOC}) qt4_wrap_ui(UI_GENERATED ${UI}) qt4_add_resources(RCC_GENERATED ${RCC}) </source>

Assign filters. Optional, but useful. <source lang=cmake> source_group("Form Files" FILES ${UI} ) source_group("Resource Files" FILES ${RCC} ) source_group("Generated Files" FILES ${MOC_GENERATED} ${UI_GENERATED} ${RCC_GENERATED} ) </source>

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

<source lang=cmake> 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 </source>

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

<source lang=cmake> 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 </source>