linuxcmakeg++vtk

How to get VTK working with G++ on linux?


I'm trying to build the VTK, I followed the steps from the GitHub Build Page but I can't locate the header files.
Where are those located? And also where is the lib which I can pass to g++ while compiling.

It has generated some files in the lib folder but I'm confused which to use with g++. Here is what's inside the lib folder

I'm running red hat enterprise linux, with AMD cpu and Nvidia GPU.

So far I have done this
mkdir vtk && cd vtk/
git clone https://gitlab.kitware.com/vtk/vtk.git

Make sure git repo is updated
git fetch origin
git rebase origin/master

mkdir VTK_build cmake ../vtk/

This is how the folder structure is:

├── vtk    
│   ├── vtk       <-- This is the git cloned folder  
│   ├── VTK-Build <-- This is where I'm building  

Solution

  • you should follow the build vtk from source and cylinder example, which uses CMake, this has worked for me using gcc on linux.

    1. build VTK (from inside the VTK-Build folder)
    cmake -S ../vtk -B .
    cmake --build .
    
    1. download the example from the tutorial site or just create a new folder and put CMakeList.txt and CylinderExample.cxx in it, for brevity i will show only the CMakeLists.txt here, please check the latest docs for an up to date syntax.
    cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
    
    project(CylinderExample)
    
    find_package(VTK COMPONENTS 
      CommonColor
      CommonCore
      FiltersSources
      InteractionStyle
      RenderingContextOpenGL2
      RenderingCore
      RenderingFreeType
      RenderingGL2PSOpenGL2
      RenderingOpenGL2
    )
    
    if (NOT VTK_FOUND)
      message(FATAL_ERROR "CylinderExample: Unable to find the VTK build folder.")
    endif()
    
    # 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}
    )
    
    1. setup your project with cmake and build it
    mkdir build
    cd build
    cmake -DVTK_DIR:PATH=path_to_vtk_build_dir ..
    cmake --build .
    
    1. make sure the built shared objects in the VTK-Build/lib are on the LD_LIBRARY_PATH environment variable before running the executable so linux can find and load those shared libraries.

    VTK is composed of a lot of shared objects depending on what functionality you want from it, you should follow the rest of the tutorials to know what components you need for a certain functionality and let cmake figure out which shared objects to link.