Linking (latest) VTK library in cmake

65 views Asked by At

enter code hereProblem:

I have code base with following structure:

root/
|
|-- main.cpp
|__ CMakeLists.txt
|
|__lib
|  |__ CMakeLists.txt
|  |__ writer.cpp
|
|__physics
   |__..

writer.cpp uses the VTK headers to write .vtk files for the simulation.

After updating VTK libraries (to 9.0.3), I am facing challanges to incorporate the VTK library while building my application.

I am using cmake for writting my build setup.

root/CMakeLists.txt

cmake_minimum_required(VERSION 3.18.0)
message(STATUS "CMake version: ${CMAKE_VERSION}")

PROJECT(root_1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ")
file(GLOB cpu_source_files "${CMAKE_SOURCE_DIR}/src/*.cpp")

#---------------------------------------------------
# Includes for VTK libraries
#---------------------------------------------------
find_package(VTK REQUIRED)

ADD_SUBDIRECTORY(lib)
include_directories(${CMAKE_SOURCE_DIR}/src/physics/)

# FINAL TARGET BUILD
vtk_module_autoinit(TARGETS myexe MODULES ${VTK_LIBRARIES})
target_include_directories(myexe PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${VTK_LIBRARIES})

root/lib/CMakeLists.txt

#---------------------------------------------------
# Includes for VTK libraries
#---------------------------------------------------

find_package(VTK REQUIRED)
# include(${VTK_USE_FILE}) # <-- not required with latest VTK

#---------------------------------------------------

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ")

#---------------------------------------------------
# Definiing the source locations etc.
#---------------------------------------------------
file(GLOB cpu_source_files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")

#---------------------------------------------------
# Building executables
#   ****** Note ******** maintain the order
#---------------------------------------------------
add_library(Writers SHARED ${cpu_source_files})
set_property(TARGET Writers PROPERTY POSITION_INDEPENDENT_CODE ON)

#---------------------------------------------------
# Include relative path during compilation
#---------------------------------------------------
vtk_module_autoinit(TARGETS Writers MODULES ${VTK_LIBRARIES})
target_include_directories(Writers PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${VTK_LIBRARIES})

The above setup leads me to the error, VTK headers (included in my writers.cpp) NOT FOUND

UPDATE ERROR WHEN BUILDING

          m_infoStepCheck == 1;
          ^

Remark: individual warnings can be suppressed with "--diag_suppress <warning-name>"

"root/lib/ioLib/simulationInputs/domainParams.cpp", line 61: warning: expression has no effect [expr_has_no_effect]
          m_writeStepCheck == 1;
          ^

[ 32%] Building CXX object lib/lib.dir/fluidParams.cpp.o
[ 35%] Building CXX object lib/lib.dir/geometryInputFileParams.cpp.o
[ 38%] Building CXX object lib/lib.dir/outputParams.cpp.o
[ 41%] Building CXX object lib/lib.dir/physicalParams.cpp.o
[ 45%] Building CXX object lib/lib.dir/restartSimulationParams.cpp.o
[ 48%] Building CXX object lib/lib.dir/simulationSettings.cpp.o
[ 51%] Linking CXX shared library lib.so
[ 51%] Built target SimulationInputs
[ 54%] Building CXX object lib/lib.dir/writers.cpp.o
"root/lib/writers.cpp", line 20: catastrophic error: cannot open source file "vtkPolyData.h"
  #include <vtkPolyData.h>
                          ^

1 catastrophic error detected in the compilation of "/root/lib/writers.cpp".
Compilation terminated.
gmake[2]: *** [lib/CMakeFiles/writers.cpp.o] Error 2
gmake[1]: *** [lib/CMakeFiles/lib.dir/all] Error 2
gmake: *** [all] Error 2

My question is

What is the standard procedure to add VTK Library (latest) to application

1

There are 1 answers

1
Protoss On

find_package(VTK REQUIRED) means use Module mode to add dependency, in Module mode you need to include VTK's headers manually, and you must setup environment variables by hand.
Example:

# Search for the header
FIND_PATH(FOO_INCLUDE_DIR "foo/foo.h"
PATHS ${_foo_HEADER_SEARCH_DIRS} )

# Search for the library
FIND_LIBRARY(FOO_LIBRARY NAMES foo
PATHS ${_foo_LIB_SEARCH_DIRS} )
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(FOO DEFAULT_MSG
FOO_LIBRARY FOO_INCLUDE_DIR)

Full example see FindXXX.cmake.

If want to include headers automatically, you should use Config mode: find_package(VTK CONFIG REQUIRED), but must write the scripts to install library.
Example:

# Targets:
#   * <prefix>/lib/libfoo.a
#   * header location after install: <prefix>/include/foo/foo.h
#   * headers can be included by C++ code `#include <foo/foo.h>`
install(
    TARGETS foo
    EXPORT "${TARGETS_EXPORT_NAME}"
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
    INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

Full example see: install