VTK CMake Usage

How does one find and use VTK in CMake with 9.0.0. I’m using find_package(VTK REQUIRED …), and it’s seems happy, although the only output appears to be finding X11 and Xt. All the documentation that I can find is conflicting, and appears to be outdated.

I would appreciate it if someone could point me to up-to-date documentation or an example. I just need to know what variable to use to get the include/library paths, and libraries.

Great timing. I just wrote up this document which details how to do it. Do you have any feedback on what else could go into it before it gets merged (it’ll be in 9.0.1)? It looks a bit weird and links are broken because it’s actually a Doxygen input (where it will ultimately end up).

I know the wiki is out-of-date; it needs a sweep to put banners about that and the still-pertinent information migrated into the repository.

1 Like

That is exactly what I was looking for, and it’s working perfectly. Thank you!

Quick question: usage of VTK_LIBRARIES is discouraged in https://gitlab.kitware.com/vtk/vtk/-/blob/1abfe43a7b32b4606964005627b6c40723883a2e/Documentation/Doxygen/ModuleMigration.md, but why exactly?
It contains all libraries needed to fulfill what was requested as components in the find_package call, right?
So why would I not just use this list, and instead again separately list the libraries in the target_link_library and the vtk_module_autoinit

If you only have one set of VTK requirements, it’s fine. But it can lead to overlinking:

add_library(needscore ...)
target_link_libraries(needscore PRIVATE VTK::CommonCore)

add_library(needsrendering ...)
target_link_libraries(needsrendering PRIVATE VTK::RenderingOpenGL2)

Using ${VTK_LIBRARIES} instead means that needscore all of a sudden ends up needing X, OpenGL, Cocoa, etc. (depending on the platform). Explicit usage is recommended so that you don’t end up adding extra links if you have another VTK-using target that needs a new component (“explicit is better than implicit”).

@ben.boeckel thanks for clarifying, makes sense!

@ben.boeckel Should a mention also be made about VTK/Utilities/Maintenance/FindNeededModules.py? This is definitely useful for determining what modules your code needs in the new system.

That is only available in the build tree (the needed JSON file is not installed), so not as immediately useful without some further work.

I didn’t know that as I always use the build tree. I must admit I find it quite useful.