File: using_cpp.md

package info (click to toggle)
vtk9 9.5.2%2Bdfsg4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 206,640 kB
  • sloc: cpp: 2,340,827; ansic: 327,116; python: 114,881; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; javascript: 1,261; makefile: 194; objc: 153; tcl: 59
file content (151 lines) | stat: -rw-r--r-- 6,481 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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](https://cmake.org).

**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 rawhide package](https://img.shields.io/fedora/v/vtk-devel) |
| Fedora 38                          |  vtk-devel      | ![Fedora 38 package](https://img.shields.io/fedora/v/vtk-devel/f38) |
| Fedora 37                          |  vtk-devel      | ![Fedora 37 package](https://img.shields.io/fedora/v/vtk-devel/f37) |
| Ubuntu 23.04 (lunar)               |  libvtk9-dev    | ![Ubuntu lunar package](https://img.shields.io/ubuntu/v/vtk9/lunar)
| Ubuntu 22.10 (kinetic)             |  libvtk9-dev    | ![Ubuntu kinetic package](https://img.shields.io/ubuntu/v/vtk9/kinetic)
| Ubuntu 22.04 (jammy)               |  libvtk9-dev    | ![Ubuntu jammy package](https://img.shields.io/ubuntu/v/vtk9/jammy)
| Ubuntu 20.04 (focal)               |  libvtk7-dev    | ![Ubuntu focal package](https://img.shields.io/ubuntu/v/vtk7/focal)
| Debian unstable                    |  libvtk9-devel  | ![Debian unstable package](https://img.shields.io/debian/v/libvtk9-dev) |
| Debian testing                     |  libvtk9-devel  | ![Debian testing package](https://img.shields.io/debian/v/libvtk9-dev/testing) |
| Debian stable                      |  libvtk9-devel  | ![Debian stable package](https://img.shields.io/debian/v/libvtk9-dev/stable) |
| Gentoo                             |     vtk         | 	[![Gentoo package](https://repology.org/badge/version-for-repo/gentoo/vtk.svg)](https://repology.org/project/vtk/versions)
| homebrew                           |     vtk         | ![homebrew version](https://img.shields.io/homebrew/v/vtk)|
| vckpg                              |     vtk         | ![Vcpkg](https://img.shields.io/vcpkg/v/vtk) |
| spack                              |     vtk         | ![Spack](https://img.shields.io/spack/v/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](../build_instructions/index.md).

**Building an executable**

Once VTK is installed using either of the methods above you can use it in your
project utilizing the
[find_package](https://cmake.org/cmake/help/latest/command/find_package.html)
infrastructure of cmake:

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

{cmake:command}`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](../api/cmake/ModuleSystem.md#autoinit).

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`](https://gitlab.kitware.com/vtk/vtk/-/blob/master/IO/XML/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](https://examples.vtk.org/site/Python/Utilities/VTKModulesForCxx) script.

For example, running the script on the [CylinderExample](https://examples.vtk.org/site/Cxx/GeometricObjects/CylinderExample)
we get the following suggestion:

```cmake
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:

```cmake
...
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](https://examples.vtk.org/site/Cxx/GeometricObjects/CylinderExample/).

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](https://kitware.github.io/vtk-examples/site/Cxx/#tutorial),
[how to guides](https://kitware.github.io/vtk-examples/site/CxxHowTo) or
[examples](https://kitware.github.io/vtk-examples/site/Cxx) sections of the vtk examples website.