Using C++ and CMake#
CMake is an open-source platform-independent build system that manages the entire software build process, from source code to executable binary. If you’re new to CMake, you can find more information on the CMake website.
Installing a binary release
Pre-built VTK releases maintained by the community exist for a number of distributions, as shown in the following table:
Operating System/ Package manager |
Package Name |
Version |
---|---|---|
Fedora Rawhide |
vtk-devel |
|
Fedora 38 |
vtk-devel |
|
Fedora 37 |
vtk-devel |
|
Ubuntu 23.04 (lunar) |
libvtk9-dev |
|
Ubuntu 22.10 (kinetic) |
libvtk9-dev |
|
Ubuntu 22.04 (jammy) |
libvtk9-dev |
|
Ubuntu 20.04 (focal) |
libvtk7-dev |
|
Debian unstable |
libvtk9-devel |
|
Debian testing |
libvtk9-devel |
|
Debian stable |
libvtk9-devel |
|
Gentoo |
vtk |
|
homebrew |
vtk |
|
vckpg |
vtk |
|
spack |
vtk |
Note that these packages may be lacking some optional features such as mpi, qt etc. or, they may not contain the latest VTK features. Check the documentation of each package to verify that the build contains what you need. If what you need is missing you will need to build vtk from scratch.
Building an executable
Once VTK is installed using either of the methods above you can use it in your project utilizing the find_package infrastructure of cmake:
find_package(VTK
COMPONENTS
.. list of vtk modules to link to
)
# your executable
add_executable(testExample ...)
# link to required VTK libraries
target_link_libraries(testExample
PRIVATE
${VTK_LIBRARIES}
)
vtk_module_autoinit(
TARGETS testExample
MODULES ${VTK_LIBRARIES}
)
vtk_module_autoinit()
is responsible for triggering static code construction required for some VTK classes.
For more details regarding the autoinit system of VTK see here.
The list of required vtk modules depends on the files #include
d in your code. The module a header file belongs to is determined
in most cases by its location in the VTK source tree. For, example vtkXMLPolyDataReader
is located under IO/XML
so it belongs to the IOXML
module,
to verify check the accompanying vtk.module
file in the same directory.
The above method works in most cases but it does not express the dependencies that some module have. A better (and easier) way to find the required modules is the VTKModulesForCxx script.
For example, running the script on the CylinderExample we get the following suggestion:
find_package(VTK
COMPONENTS
CommonColor
CommonCore
FiltersSources
RenderingCore
#
# These modules are suggested since they implement an existing module.
# You may need to uncomment one or more of these.
# If vtkRenderWindow is used and you want to use OpenGL,
# you also need the RenderingOpenGL2 module.
# If vtkRenderWindowInteractor is used,
# uncomment RenderingUI and possibly InteractionStyle.
# If text rendering is used, uncomment RenderingFreeType
#
# InteractionStyle # implements VTK::RenderingCore
# RenderingCellGrid # implements VTK::RenderingCore
# RenderingFreeType # implements VTK::RenderingCore
# RenderingOpenGL2 # implements VTK::RenderingCore
# RenderingUI # implements VTK::RenderingCore
)
Based on the suggestions of the script and the template above the relevant sections of the CMakeLists.txt
are:
...
find_package(VTK COMPONENTS
CommonColor
CommonCore
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
add_executable(CylinderExample CylinderExample.cxx)
target_link_libraries(CylinderExample PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS CylinderExample
MODULES ${VTK_LIBRARIES}
)
The full source of the example can be found here.
To build the example:
mkdir build
cd build
ccmake ../ # or cmake-gui if on Windows
Hit C
if using ccmake
or the configure button if using cmake-gui
.
If VTK was built from scratch you will need to set VTK_DIR
to the installation path.
If ccmake
/cmake-gui
reports no errors quit ccmake
/cmake-gui
and build the project as follows:
cmake --build .
To run the example
./CylinderExample
For more examples check the tutorials, how to guides or examples sections of the vtk examples website.