Skip to content

CheckVTKVersion

vtk-examples/Cxx/Utilities/CheckVTKVersion

Description

This example demonstrates how to check the VTK version with vtkVersion.

Also demonstrated is the usage of the various version macros.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

CheckVTKVersion.cxx

#include <vtkVersion.h>

#ifdef VTK_VERSION_NUMBER
#if VTK_VERSION_NUMBER >= 90000000000ULL
#define VTK_VER_GE_90 1
#endif
#else
#define VTK_VERSION_NUMBER_NOT_DEFINED 1
#endif

namespace {
/**
 * Check the VTK version.
 *
 * @param major: Major version.
 * @param major: Minor version.
 * @param major: Build version.
 *
 * @return True if the requested VTK version is greater or equal to the actual
 * VTK version.
 */
bool VTKVersionOk(unsigned long long const& major,
                  unsigned long long const& minor,
                  unsigned long long const& build);

} // namespace

int main(int, char*[])
{
#ifdef VTK_VERSION_NUMBER
  std::cout << "Source version: " << vtkVersion::GetVTKSourceVersion()
            << std::endl;
  std::cout << "Major Version : " << vtkVersion::GetVTKMajorVersion()
            << std::endl;
  std::cout << "Minor Version : " << vtkVersion::GetVTKMinorVersion()
            << std::endl;
#elif VTK_VERSION_NUMBER_NOT_DEFINED
  std::cout << "VTK VTK_VERSION_NUMBER is not defined." << std::endl;
  std::cout << "VTK Major Version: " << VTK_MAJOR_VERSION << std::endl;
  std::cout << "VTK Minor Version: " << VTK_MINOR_VERSION << std::endl;
#endif

#ifdef VTK_VER_GE_90
  std::cout << "VTK Version is >= " << VTK_VERSION_NUMBER << std::endl;
#endif

  if (!VTKVersionOk(8, 90, 0))
  {
    std::cerr << "You need VTK version 8.90 or greater to run this program."
              << std::endl;
    return EXIT_FAILURE;
  }
  else
  {
    std::cout << "You have the needed VTK version" << endl;
  }

  return EXIT_SUCCESS;
}

namespace {
bool VTKVersionOk(unsigned long long const& major,
                  unsigned long long const& minor,
                  unsigned long long const& build)
{
  unsigned long long neededVersion =
      10000000000ULL * major + 100000000ULL * minor + build;
#ifndef VTK_VERSION_NUMBER
  auto ver = vtkSmartPointer<vtkVersion>();
  unsigned long long vtk_version_number =
      10000000000ULL * ver->GetVTKMajorVersion() +
      100000000ULL * ver->GetVTKMinorVersion() + ver->GetVTKBuildVersion();
  if (vtk_version_number >= neededVersion)
  {
    return true;
  }
  return false;
#else
  if (VTK_VERSION_NUMBER >= neededVersion)
  {
    return true;
  }
  return false;
#endif
}

} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(CheckVTKVersion)

find_package(VTK COMPONENTS 
  vtkCommonCore
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping CheckVTKVersion: ${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(CheckVTKVersion MACOSX_BUNDLE CheckVTKVersion.cxx )
  target_link_libraries(CheckVTKVersion PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(CheckVTKVersion MACOSX_BUNDLE CheckVTKVersion.cxx )
  target_link_libraries(CheckVTKVersion PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS CheckVTKVersion
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build CheckVTKVersion

Click here to download CheckVTKVersion and its CMakeLists.txt file. Once the tarball CheckVTKVersion.tar has been downloaded and extracted,

cd CheckVTKVersion/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./CheckVTKVersion

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.