Using part of VTK in my own project?

Hello!

So this is my first time trying to achieve something like this. What I want to is basically to use some functionality provided in this example in my own project;

https://lorensen.github.io/VTKExamples/site/Cxx/IO/WriteVTP/

I have managed to build VTK and know that I can run the example without any errors, so every header is functional etc.

What I think I have to do is to use cmake in something like this:

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(MyProject VERSION 1.0.0)

add_library(

AllTheCode

Bi4File.cpp

Bi4File.h

bi4read.c

bi4read.h

Block.cpp

Block.h

BlockHelper.h

drawGeom.h

Geometry.h

horspool.c

horspool.h

Particle.cpp

Particle.h

Statistics.h

SubBlock.cpp

SubBlock.h

Tuple.h

Vector.h

)

#a.out is executable

add_executable(a.out main.cpp)

set(LIBS AllTheCode)

target_link_libraries(a.out PRIVATE ${LIBS})

So this code is all the code except the vtk example and I am very unsure of how to include it, since it needs its own CMakeLists.txt to even compile. I have not been succesful in doing this but I am doing something like:

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(WriteVTP)

SET(VTK_DIR “/home/aras/vtk-build/” CACHE PATH “VTK directory override” FORCE)

find_package(VTK COMPONENTS

CommonCore

CommonDataModel

IOXML)

if (NOT VTK_FOUND)

message(“Skipping WriteVTP: ${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(WriteVTP MACOSX_BUNDLE WriteVTP.cxx )

target_link_libraries(WriteVTP PRIVATE ${VTK_LIBRARIES})

else ()

include all components

add_executable(WriteVTP MACOSX_BUNDLE WriteVTP.cxx )

target_link_libraries(WriteVTP PRIVATE ${VTK_LIBRARIES})

vtk_module_autoinit is needed

vtk_module_autoinit(

TARGETS WriteVTP

MODULES ${VTK_LIBRARIES}

)

endif ()

And when ever I try to include it, it ends up not working. Could anyone help me to get this up and running?

To sum up again; I want to use this specific vtk functionality in my own code - unsure how to go about that and cannot make CMake accept it, so I can use the function in my own code. If I just add it as a “header file” it can’t find the vtk headers correctly.

Kind regards

Am I the first to want to do, what I am writing about or maybe I was not clear enough initially?

Basically what I have now is:

image

Which generates a working executable. But I am not able to link it to my main project. Any suggestions?

Kind regards

Please, copy paste the commands you issued and the error you got, directly from the terminal.

Sure :slight_smile: So this is what I have right now:

For the VTK file generation

For the whole project:

And when I compile the error I get right now is:

I think I am pretty close… just having some small misunderstanding I guess.

Kind regards

Okay, so I actually got it to build succesfully now by changing in “VTK file generation”, at line 11, by including “SHARED” keyword as such:

image

And it seems to link properly to my main program now. But I am a bit unsure on how to call a function from a .so file in C++?

Basically the function looks like this:

But how would I use this “func” in my main file for example?

Kind regards