Building a library which depends on VTK: Headers not found.

All–

I’m trying to build a library which depends upon VTK, but am having trouble getting it to find the header files. I’ve put together a minimal example to show the error:

$ make
Scanning dependencies of target dv-utils-lib
[ 50%] Building CXX object CMakeFiles/dv-utils-lib.dir/src/dvTest.cxx.o
In file included from /Users/davisvigneault/Desktop/src/src/dvTest.cxx:1:
/Users/davisvigneault/Desktop/src/includes/dvTest.h:4:10: fatal error: 'vtkMatrix4x4.h' file not found
#include <vtkMatrix4x4.h>
         ^~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/dv-utils-lib.dir/src/dvTest.cxx.o] Error 1
make[1]: *** [CMakeFiles/dv-utils-lib.dir/all] Error 2
make: *** [all] Error 2

Here’s CMakeLists.txt:

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(dv-commandline-utils)
find_package(VTK REQUIRED)
include_directories("includes/")
file(GLOB SRC src/*.cxx)
add_library(dv-utils-lib ${SRC})

Here’s includes/dvTest.h

#ifndef dv_Test_h
#define dv_Test_h

#include <vtkMatrix4x4.h>

double GetFirstElement(vtkMatrix4x4 *mat);

#endif

Here’s src/dvTest.cxx:

#include <dvTest.h>
#include <vtkMatrix4x4.h>

double GetFirstElement(vtkMatrix4x4 mat*) {
  return mat->GetElement(0,0);
}

I’m on Mac 10.14.5, on the master branch of VTK. Apologies if I’m missing something simple, and thanks in advance for any help!

Best, and thanks,

–Davis

The common pattern for VTK and any other package is this:

find_package(VTK REQUIRED #[[COMPONENTS vtkFiltersSources vtkIOLegacy ... NO_MODULE]])

if(VTK_FOUND)
    include(${VTK_USE_FILE}) # this is important

    add_library(${PROJECT_NAME} #[[...]])
    target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES})
endif()

Ron–

Thanks for your response. I had previously been using include(${VTK_USE_FILE}), but upon updating to the master branch I began getting an error about there being an incorrect number of arguments to the include function. Is ${VTK_USE_FILE} no longer being set? I gather from Dr. Lorensen’s examples on github [1] that this functionality has somehow been replaced by vtk_module_autoinit in recent versions of VTK.

Ultimately, I was able to get it all working (building a library that depends on VTK, and then building an executable which depends both on VTK and on my library) with something similar to this:

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(dv-commandline-utils)

find_package(VTK REQUIRED)

include_directories("includes/")
file(GLOB SRC src/*.cxx)
add_library(dv-utils-lib ${SRC})
target_link_libraries(dv-utils-lib ${VTK_LIBRARIES})

add_executable(dv-visualize-meshes dv-visualize-meshes.cxx)
target_link_libraries(dv-visualize-meshes ${VTK_LIBRARIES} dv-utils-lib)

vtk_module_autoinit(
  TARGETS dv-visualize-meshes
  MODULES ${VTK_LIBRARIES}
  )

Thanks very much for your help,

–Davis

My mistake. But this is the old problem with VTK. Nothing is well documented…

Ron–

No worries–your response nonetheless pointed me down the right path. At least what the ITK/VTK echosystem occasionally lacks in documentation, it makes up for with an active and supportive community!

Best, and thanks again,

–Davis

Since you are using the VTK Master, try using: Utilities/Maintenance/FindNeededModules.py.
This will produce an output something like this that only lists the modules needed by your library:

find_package(VTK
 COMPONENTS
    CommonColor
    CommonCore
    CommonDataModel
    CommonTransforms
    FiltersGeneral
    FiltersModeling
    FiltersSources
    IOGeometry
    IOLegacy
    IOPLY
    IOXML
    RenderingCore
    # These modules are suggested since they implement an existing module.
    # Uncomment those you need.
    # InteractionStyle  # implements VTK::RenderingCore
    # RenderingFreeType # implements VTK::RenderingCore
    # RenderingOpenGL2  # implements VTK::RenderingCore
)

that you can drop into your CMake file.

Then …
Note: I haven’t tried this!.

  add_library(dv-utils-lib MACOSX_BUNDLE ${SRC} )
  target_link_libraries(dv-utils-lib PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS dv-utils-lib
    MODULES ${VTK_LIBRARIES}
    )

ParaView is in its release cycle and has gotten documentation love. VTK will get the same before the release happens. There’s developer-level documentation, but user-level docs are missing right now.

Hey @amaclean very cool script! I’ve never known the right way to go about figuring out which components are needed.

I tried running it on my project, and it gave me the following:

find_package(VTK
 COMPONENTS
    CommonCore
    CommonDataModel
    CommonMath
    CommonTransforms
    FiltersCore
    FiltersExtraction
    FiltersGeneral
    FiltersModeling
    FiltersSources
    IOGeometry
    IOImage
    IOLegacy
    ImagingColor
    InteractionImage
    InteractionWidgets
    RenderingAnnotation
    RenderingCore
    RenderingOpenGL2
    RenderingVolumeOpenGL2
    # These modules are suggested since they implement an existing module.
    # Uncomment those you need.
    # InteractionStyle      # implements VTK::RenderingCore
    # RenderingFreeType     # implements VTK::RenderingCore
    # RenderingGL2PSOpenGL2 # implements VTK::RenderingOpenGL2
)

Looks good, but now it unfortunately can’t find my Qt headers. I expect that it’s related to the rendering core (see commented at the bottom of the script), but I’ve tried uncommenting each in turn, with no luck. Is there something special I need to do to get this working with Qt?

And again, thanks so much for your help.

Best,

–Davis

You will have to manually add other components. Tthe script only looks for vtk includes that start with lower-case vtk e.g. vtkSmartPointer.h