VTK/Build System Migration

From KitwarePublic
< VTK
Revision as of 02:15, 11 September 2012 by Marcus.hanwell (talk | contribs) (Initial pass at build system migration document aimed at VTK users)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Building Against VTK 6

If you are building against VTK 6.0 a few things have changed as a result of the modularization and updates to the CMake infrastructure.

Finding and Linking to VTK

If you just want to find VTK and link to everything that was built not too much has changed. A simple CMakeLists.txt to accomplish this and build an application would be:

<source lang="cmake"> find_package(VTK 6.0 REQUIRED) include(${VTK_USE_FILE})

add_executable(myApplication application.cxx) target_link_libraries(myApplication ${VTK_LIBRARIES}) </source>

This will work against a VTK build tree, or an installed VTK tree. It will find all modules built by VTK, and link to all of the C++ libraries produced. Including the VTK_USE_FILE will set up the include directories for the project, and add the appropriate compiler definitions to automatically initialize the object factory overrides. This is a reasonable starting point, but is not normally desired for applications in production. It is quite easy to find and link to only the components required, ensuring that they were built and only linking to the appropriate libraries.

<source lang="cmake"> find_package(VTK 6.0 COMPONENTS vtkChartsCore vtkGUISupportQt vtkViewsContext2D) include(${VTK_USE_FILE})

add_executable(myApplication application.cxx) target_link_libraries(myApplication ${VTK_LIBRARIES}) </source>

The above snippet will find the vtkChartsCore, vtkGUISupportQt and vtkViewsContext2D, along with all of their dependencies. It will only add their include directories (along with their dependencies), and the appropriate compiler definitions to initialize the object factories. This is probably the most appropriate way to find and link to VTK 6 in the majority of projects.

It is possible to manually specify the libraries to be linked to, avoiding the directory scope compiler definitions and include directories, as all of the appropriate settings are loaded into CMake variables.

<source lang="cmake"> find_package(VTK 6.0 COMPONENTS vtkChartsCore vtkGUISupportQt vtkViewsContext2D) add_definitions(${VTK_DEFINITIONS}) include_directories(SYSTEM ${VTK_INCLUDE_DIRS}) add_executable(myApplication application.cxx) target_link_libraries(myApplication vtkChartsCore vtkGUISupportQt vtkViewsContext2D) </source>

The VTK libraries are exported by CMake, and so can be referred to as any normal target. The correct library will be linked to for debug/release, along with any required interface libraries. The find_package(VTK) command is safe to call multiple times in the same project, and allows different components to be selected in different applications or libraries in the same project.