Can't build VTK sample project: Missing module

I just edited my CMakeLists.txt, it now looks like this:

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_TOOLCHAIN_FILE /home/ilmu011/Desktop/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "")
set(VCPKG_TARGET_TRIPLET "x64-linux" CACHE STRING "")

set(CMAKE_PREFIX_PATH
/home/ilmu011/Desktop/vcpkg/installed/x64-linux/share/vtk
)

set(CMAKE_BUILD_TYPE Release)

set(FREETYPE_INCLUDE_DIR_freetype2 "/usr/include/freetype2")
set(FREETYPE_INCLUDE_DIR_ft2build "/usr/include/freetype2")
set(FREETYPE_LIBRARY_RELEASE "/usr/lib/x86_64-linux-gnu/libfreetype.so")

project(CylinderExample, LANGUAGES CXX)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkFiltersSources
  vtkInteractionStyle
  # vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  # vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping CylinderExample: ${VTK_NOT_FOUND_MESSAGE}")
  return()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(CylinderExample MACOSX_BUNDLE CylinderExample.cxx )
  target_link_libraries(CylinderExample PRIVATE ${VTK_LIBRARIES})
else()
  # Prevent a "command line is too long" failure in Windows.
  set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
  add_executable(CylinderExample MACOSX_BUNDLE CylinderExample.cxx )
  target_link_libraries(CylinderExample PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS CylinderExample
    MODULES ${VTK_LIBRARIES}
    )
endif()

However, the INCLUDE_DIRS seem to not have been apllied correctly… from the Cache:

//Path to a file.
FREETYPE_INCLUDE_DIR_freetype2:PATH=

//Path to a file.
FREETYPE_INCLUDE_DIR_ft2build:PATH=

//Path to a library.
FREETYPE_LIBRARY_DEBUG:FILEPATH=/home/ilmu011/Desktop/vcpkg/installed/x64-linux/debug/lib/libfreetyped.a

//Path to a file.
Fontconfig_INCLUDE_DIR:PATH=/home/ilmu011/Desktop/vcpkg/installed/x64-linux/include

How do I do this correctly?

The easiest way (quick and dirty) to set the cache variables is to edit the CMakeCache.txt file directly, and then re-run cmake. But this has to be repeated each time you do a clean build, so it’s not ideal.

Another way is by passing multiple -D values when you run cmake, one for each variable. This can be tedious, but you can put the whole cmake command-line into a bash script to make it reusable.

For putting cache variables into a CMakeLists.txt, it’s necessary to use the CACHE keyword in the set() command:

set(CMAKE_BUILD_TYPE Release CACHE STRING "")

syntax: (note that <docstring> can be an empty string)

set(<variable> <value> CACHE <type> <docstring> [FORCE])

Instead of putting these in the CMakeLists.txt, they can be put in their own cmake file, e.g. a file called cache.cmake that is used like this:

cmake -C cache.cmake ...

where cache.cmake contains

set(CMAKE_BUILD_TYPE Release CACHE STRING "")
set(CMAKE_PREFIX_PATH
/home/ilmu011/Desktop/vcpkg/installed/x64-linux/share/vtk
CACHE PATH ""
)

It’s verbose, which is why using -D is more common.

Make sure that you have libfreetype-dev installed, it’s possible that cmake didn’t automatically find the system libs and headers for freetype because they’re not installed on your system.

Hopefully someone else can answer the vcpkg questions, because I don’t really know anything about it, except that it exists.

1 Like

Thanks for your detailed answer.

If I wanted to pass multiple arguments with the -D option in the command line, how would I do that? I thought I just had to sperate them with a semicolon, but this didn’t work:

cmake -S source -B build -DFREETYPE_INCLUDE_DIR_freetype2:PATH=/usr/include/freetype2;FREETYPE_INCLUDE_DIR_ft2build:PATH=/usr/include/freetype2;FREETYPE_LIBRARY_DEBUG:FILEPATH=FREETYPE_LIBRARY_DEBUG-NOTFOUND;FREETYPE_LIBRARY_RELEASE:FILEPATH=/usr/lib/x86_64-linux-gnu/libfreetype.so

I now managed to do it like this:

cmake -S source -B build -DFREETYPE_INCLUDE_DIR_freetype2:PATH=/usr/include/freetype2 -DFREETYPE_INCLUDE_DIR_ft2build:PATH=/usr/include/freetype2 -DFREETYPE_LIBRARY_DEBUG:FILEPATH=FREETYPE_LIBRARY_DEBUG-NOTFOUND -DFREETYPE_LIBRARY_RELEASE:FILEPATH=/usr/lib/x86_64-linux-gnu/libfreetype.so

No more errors or warning occur, when executing the cmake or make command, and the program is built successfully, however, I get a runtime error when executing the program:

ilmu011@ilmu011-VirtualBox:~/Desktop/CylinderExample/build$ ./CylinderExample 
2022-03-15 21:38:14.341 (   0.998s) [        D169A740]vtkOpenGLRenderWindow.c:464    ERR| vtkXOpenGLRenderWindow (0x55c2929fbd80): Unable to find a valid OpenGL 3.2 or later implementation. Please update your video card driver to the latest version. If you are using Mesa please make sure you have version 11.2 or later and make sure your driver in Mesa supports OpenGL 3.2 such as llvmpipe or openswr. If you are on windows and using Microsoft remote desktop note that it only supports OpenGL 3.2 with nvidia quadro cards. You can use other remoting software such as nomachine to avoid this issue.
2022-03-15 21:38:14.341 (   0.998s) [        D169A740]     vtkOpenGLState.cxx:1785  WARN| Hardware does not support the number of textures defined.
2022-03-15 21:38:14.341 (   0.998s) [        D169A740]     vtkOpenGLState.cxx:1785  WARN| Hardware does not support the number of textures defined.
2022-03-15 21:38:14.342 (   0.998s) [        D169A740]     vtkOpenGLState.cxx:1785  WARN| Hardware does not support the number of textures defined.
2022-03-15 21:38:14.342 (   0.998s) [        D169A740]     vtkOpenGLState.cxx:1785  WARN| Hardware does not support the number of textures defined.
2022-03-15 21:38:14.342 (   0.998s) [        D169A740]     vtkOpenGLState.cxx:1785  WARN| Hardware does not support the number of textures defined.
2022-03-15 21:38:14.342 (   0.999s) [        D169A740]     vtkOpenGLState.cxx:1785  WARN| Hardware does not support the number of textures defined.
2022-03-15 21:38:14.342 (   0.999s) [        D169A740]     vtkOpenGLState.cxx:1785  WARN| Hardware does not support the number of textures defined.
2022-03-15 21:38:14.366 (   1.023s) [        D169A740]   vtkShaderProgram.cxx:437    ERR| vtkShaderProgram (0x55c292be6110): 1: #version 150
2: #ifndef GL_ES
3: #define highp
4: #define mediump
5: #define lowp
6: #endif // GL_ES
7: #define attribute in
8: #define varying out
9: 
10: 
11: /*=========================================================================
12: 
13:   Program:   Visualization Toolkit
14:   Module:    vtkPolyDataVS.glsl
15: 
16:   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
17:   All rights reserved.
18:   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
19: 
20:      This software is distributed WITHOUT ANY WARRANTY; without even
21:      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22:      PURPOSE.  See the above copyright notice for more information.
23: 
24: =========================================================================*/
25: 
26: in vec4 vertexMC;
27: 
28: 
29: 
30: // frag position in VC
31: out vec4 vertexVCVSOutput;
32: 
33: // optional normal declaration
34: //VTK::Normal::Dec
35: in vec3 normalMC;
36: uniform mat3 normalMatrix;
37: out vec3 normalVCVSOutput;
38: 
39: // extra lighting parameters
40: //VTK::Light::Dec
41: 
42: // Texture coordinates
43: //VTK::TCoord::Dec
44: 
45: // material property values
46: //VTK::Color::Dec
47: 
48: // clipping plane vars
49: //VTK::Clip::Dec
50: 
51: // camera and actor matrix values
52: uniform mat4 MCDCMatrix;
53: uniform mat4 MCVCMatrix;
54: 
55: // Apple Bug
56: //VTK::PrimID::Dec
57: 
58: // Value raster
59: //VTK::ValuePass::Dec
60: 
61: // picking support
62: //VTK::Picking::Dec
63: 
64: void main()
65: {
66:   //VTK::CustomBegin::Impl
67: 
68:   //VTK::Color::Impl
69: 
70:   normalVCVSOutput = normalMatrix * normalMC;
71: //VTK::Normal::Impl
72: 
73:   //VTK::TCoord::Impl
74: 
75:   //VTK::Clip::Impl
76: 
77:   //VTK::PrimID::Impl
78: 
79:   vertexVCVSOutput = MCVCMatrix * vertexMC;
80:   gl_Position = MCDCMatrix * vertexMC;
81: 
82: 
83:   //VTK::ValuePass::Impl
84: 
85:   //VTK::Light::Impl
86: 
87:   //VTK::Picking::Impl
88: 
89:   //VTK::CustomEnd::Impl
90: }

2022-03-15 21:38:14.368 (   1.024s) [        D169A740]   vtkShaderProgram.cxx:438    ERR| vtkShaderProgram (0x55c292be6110): 0:1(10): error: GLSL 1.50 is not supported. Supported versions are: 1.10, 1.20, and 1.00 ES

Segmentation fault (core dumped)

This is the reason for the runtime error:

Are you running on a VM? Most VM’s don’t provide sufficient GPU support for OpenGL 3.2, which is required by VTK for rendering. But you can try forcing OpenGL to use software rendering, so that it doesn’t rely on the VM’s GPU.

Set this environment variable before running the example:

export LIBGL_ALWAYS_SOFTWARE=true

Older versions of VTK, e.g. VTK 7.1, did not require OpenGL 3.2 and were easier to run in VMs and over VNC remote connections.

Yes, I am running in a VM, but setting that variable did not solve the problem

Also, after doing a quick search, it seems like versioning with vcpkg seems to be very complex and inconvenient… I don’t know if I can downgrade the VTK version using vcpkg

Hello, friend,

If you feel like trying an alternative to vcpkg, I suggest you to try Conan: https://conan.io/ . There is a so-called Conan recipe (a Python script) that works just fine automating building VTK with CMake. The same applies to whatever dependency your software may have.

regards,

Paulo

1 Like

I have heard about Conan for some while now and I have heard that it is a viable alternative to vcpkg. Maybe I will look into this. Thanks for your suggestion.

Well, that was a bit of understatement. We use Conan to manage complex dependency trees when building crossplatform software, so it’s far beyond viable. Of course, using it will introduce yet another script layer over CMake, QMake, Autotools, etc. It only pays off if you have 10 or more dependencies to manage. Otherwise, it is easier to just build them separately or relying on some package manager to get precompiled libraries and headers. In Windows, MSYS2 has pacman that can download the so-called devel packages (headers and link-time libraries in addition to the runtime libraries).