Problem with using 【CMakeLists】 to run 【VMTK c++】 code

Dear All,
In my project, I am to run a VMTK c++ code using CMake. My setup are
【linux mint】+ 【gcc】+【visual studio code】+【cmake】, and first serveral lines of code are at the very bottom:
The problem I have is with CMakeLists.txt. To run the code, here are three ways I have tried:
【1】 I download the vmtk source code and build with CMake, and tree structure is as this
Screenshot from 2023-02-20 22-12-04
where include/vmtk and include/vtk-9.1 contain header files, and install/lib contains all the shared libs.

and CMakeLists.txt is as this

Blockquote
cmake_minimum_required(VERSION 3.0.0)
project(VMTK_TOY VERSION 0.1.0)
include(CTest)
enable_testing()
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
#/executable/
add_executable(${PROJECT_NAME} main.cpp)
#/headerfiles/
target_include_directories( ${PROJECT_NAME} PRIVATE XXX/vmtk-build/Install/include/vtk-9.1)
target_include_directories( ${PROJECT_NAME} PRIVATE XXX/vmtk-build/Install/include/vmtk )
#/vmtk shared libs/
set( VMTK_DIR “XXX/vmtk-build/Install/lib” )
target_link_libraries(${PROJECT_NAME} PRIVATE
${VMTK_DIR}/libvtkvmtkCommon.so
${VMTK_DIR}/libvtkvmtkComputationalGeometry.so
${VMTK_DIR}/libvtkvmtkContrib.so
${VMTK_DIR}/libvtkvmtkDifferentialGeometry.so
${VMTK_DIR}/libvtkvmtkIO.so
${VMTK_DIR}/libvtkvmtkITK.so
${VMTK_DIR}/libvtkvmtkMisc.so
${VMTK_DIR}/libvtkvmtkRendering.so
${VMTK_DIR}/libvtkvmtkSegmentation.so)
#/vtk shared libs/
find_package(VTK
COMPONENTS
CommonCore
CommonDataModel
CommonTransforms
FiltersCore
FiltersGeneral
FiltersGeometry
FiltersModeling
IOImage
IOXML
ImagingCore
ImagingStatistics
InteractionStyle
RenderingCore
RenderingVolume
RenderingOpenGL2
RenderingVolumeOpenGL2
OPTIONAL_COMPONENTS
TestingCore
TestingRendering)
target_link_libraries(${PROJECT_NAME} PRIVATE ${VTK_LIBRARIES})

then i got error like this.
collect2: error: ld returned 1 exit status

vtk shared libs part on this CMakelist.txt are copy and past from cmakelists.txt of a vtk example.
Then I realized that, this way, cmake might have linked to my other vtk-build (Which I have built years earlier, and stored at /usr/local/lib)
So I tried to link every single .so or .so.* files directly
【2】

Blockquote
cmake_minimum_required(VERSION 3.0.0)
project(VMTK_TOY VERSION 0.1.0)
include(CTest)
enable_testing()
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories( ${PROJECT_NAME} PRIVATE XXX/vmtk-build/Install/include/vtk-9.1)
target_include_directories( ${PROJECT_NAME} PRIVATE XXX/vmtk-build/Install/include/vmtk )
target_link_directories( ${PROJECT_NAME} PRIVATE
XXX/vmtk-build/Install/lib/libITKBiasCorrection-5.2.so
XXX/vmtk-build/Install/lib/libITKBiasCorrection-5.2.so.1
XXX/vmtk-build/Install/lib/libITKColormap-5.2.so

)

basically, in target_link_directories, I listed every sinlge .so or .so.* files, in the directory
XXX/vmtk-build/install/lib

still, I got error like this

In fear of vmtk that I built may have flaws, I tried to install vmtk through anaconda
【3】
under

Blockquote
XXX/anaconda3/pkg

there are vmtk-1.4.0-py36**** and vtk-8.10-py36*** folders, both are like this
Screenshot from 2023-02-20 23-54-37
so I changed target_link_directories and target_include_directories in method【2】(without the rest changed)
to

Blockquote
target_include_directories( ${PROJECT_NAME} PRIVATE XXX/anaconda3/pkg/vtk-8.1.0-py36***/include/vtk-8.1)
target_include_directories( ${PROJECT_NAME} PRIVATE XXX/anaconda3/pkg/vmtk/include/vmtk )
target_link_directories( ${PROJECT_NAME} PRIVATE
XXX/anaconda3/pkg/vtk-8.1.0-py36***/lib/libITKBiasCorrection-5.2.so
XXX/anaconda3/pkg/vtk-8.1.0-py36***/lib/libITKBiasCorrection-5.2.so.1
XXX/anaconda3/pkg/vtk-8.1.0-py36***/lib/libITKColormap-5.2.so

)

and get the same error as 【1】
I have double checked every single path, and there is no typo on the paths.
So I doubt, the shared libs are still not linked in cmakelists.txt

On Windows 10, I tried this code using Visual Studio, it include headers and link .dll successfully, and the code runs well. Yet, I did not try running this code using CMake on Windows.

I researched on this issue for the whole weekend, but could not get a solution.
Please help me if you have any ideas or suggestions.
Thanks in advance!!!

Here is the c++ code.

Blockquote
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkJPEGReader.h>
#include <vtkCamera.h>
#include <vtkProperty.h>
#include <vtkAxesActor.h>
#include <vtkSTLReader.h>
#include <vtkDiscreteMarchingCubes.h>
#include <vtkWindowedSincPolyDataFilter.h>
#include <vtkCleanPolyData.h>
#include <vtkTriangleFilter.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkDataArray.h>
#include <vtkIdList.h>
#include <vtkDecimatePro.h>
#include <vtkDoubleArray.h>
#include <vtkParametricSpline.h>
#include <vtkParametricFunctionSource.h>
#include <vtkLODActor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkvmtkCapPolyData.h>
#include <vtkvmtkPolyDataCenterlines.h>
#include<vtkSTLWriter.h>
#include<vtkConeSource.h>
#include
#include
using namespace std;
int main()
{
string stl = “rightlumen.stl”;
vtkSmartPointer reader = vtkSmartPointer::New();
reader->SetFileName(stl.c_str());
reader->Update();
vtkSmartPointer data = vtkSmartPointer::New();
data->DeepCopy(reader->GetOutput());
return 0;
}

Hello @tigerhu7. As I see the problem it seems that VMTK configuration files do not account for the ITK and VTK used in the superbuild process. I have opened an issue (Using VMTK build tree for development · Issue #448 · vmtk/vmtk · GitHub) and a PR (ENH: Add ITK and VTK to buildtree VMTKConfig.cmake by RafaelPalomar · Pull Request #449 · vmtk/vmtk · GitHub). It woudl be nice if @lassoan, @pieper or @jcfr could comment on whether this is an appropriate way to solve the issue.

Using that PR (you can pull it yourself and test it) I was able to build the following project, which was based on your question:

 # CMake project definition
cmake_minimum_required(VERSION 3.0.0)
project(VMTK_TOY VERSION 0.1.0)

find_package(VMTK REQUIRED)
include(${VMTK_USE_FILE})

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE ${VTK_LIBRARIES})
// main.cpp file
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkJPEGReader.h>
#include <vtkCamera.h>
#include <vtkProperty.h>
#include <vtkAxesActor.h>
#include <vtkSTLReader.h>
#include <vtkDiscreteMarchingCubes.h>
#include <vtkWindowedSincPolyDataFilter.h>
#include <vtkCleanPolyData.h>
#include <vtkTriangleFilter.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkDataArray.h>
#include <vtkIdList.h>
#include <vtkDecimatePro.h>
#include <vtkDoubleArray.h>
#include <vtkParametricSpline.h>
#include <vtkParametricFunctionSource.h>
#include <vtkLODActor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkvmtkCapPolyData.h>
#include <vtkvmtkPolyDataCenterlines.h>
#include<vtkSTLWriter.h>
#include<vtkConeSource.h>
using namespace std;
int main()
{
  string stl = "rightlumen.stl";
  auto reader = vtkSmartPointer<vtkSTLReader>::New();
  reader->SetFileName(stl.c_str());
  reader->Update();
  return 0;
}

Your sample project did not include any of the VMTK components, though, so you should try with a more complete project.

I hope this helps.

1 Like

Dear @RafaelPalomar ,
Thank you for your kind reply!!!
I figured it out just a moment ago. Yes, it is the problem with VTK configuration. Here is what I have done:
[1] rebuilt vmtk
[2] rewrite CMakeLists.txt as

cmake_minimum_required(VERSION 3.0.0)
project(VMTK_TOY VERSION 0.1.0)
include(CTest)
enable_testing()
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
set(VTK_DIR "XXX/vmtk-superbuild/VTK-Build")

# find vtk
find_package(VTK
  COMPONENTS
    CommonCore
    CommonDataModel
    CommonTransforms
    FiltersCore
    FiltersGeneral
    FiltersGeometry
    FiltersModeling
    IOImage
    IOXML
    ImagingCore
    ImagingStatistics
    InteractionStyle
    RenderingCore
    RenderingVolume
    RenderingOpenGL2
    RenderingVolumeOpenGL2
  OPTIONAL_COMPONENTS
    TestingCore
    TestingRendering)

# header files
include_directories(
"XXX/vmtk-superbuild/Install/include/vtk-9.1" 
"XXX/vmtk-superbuild/Install/include/vmtk" 
)

# executable
add_executable(${PROJECT_NAME} main.cpp)

# shared files
target_link_libraries(VMTK_TOY
XXX/vmtk-superbuild/Install/lib/*.so*
(i.e. every *.so* files)
}

The difference part of my CMakeLists.txt is that I wrote directories explicitly.
I will try your method, too.
Thanks again for your kind help!!

1 Like

Dear @RafaelPalomar
I have tested your CMakeLists.txt file, unfortunately it did not work for me.
If I use yours, I got this error

 fatal error: vtkAutoInit.h: No such file or directory
[build]     3 | #include <vtkAutoInit.h>
[build]       |          ^~~~~~~~~~~~~~~
[build] compilation terminated.

It seems the header file is not included.
Then I have modified your CMakeLists.txt in several ways, the best I have right now like this

cmake_minimum_required(VERSION 3.0.0)
project(VMTK_TOY VERSION 0.1.0)
set(VMTK_DIR "XXX/ThirdParties/vmtk-superbuild/VMTK-Build")
set(VTK_DIR "XXX/ThirdParties/vmtk-superbuild/VTK-Build")

find_package(VTK REQUIRED CONFIG PATHS "XXX/ThirdParties/vmtk-superbuild/VTK-Build" NO_DEFAULT_PATH)
find_package(VMTK REQUIRED  PATHS "XXX/ThirdParties/vmtk-superbuild/VMTK-Build" NO_DEFAULT_PATH)

message(STATUS, "VTK DIR = ${VTK_DIR}")
message(STATUS, "VMTK DIR = ${VMTK_DIR}")
include(${VMTK_USE_FILE} ${VTK_USE_FILE})
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE ${VTK_LIBRARIES} )
target_link_libraries(${PROJECT_NAME} PRIVATE ${VMTK_LIBRARIES} )

Still it says this error.

[build] Consolidate compiler generated dependencies of target VMTK_TOY
[build] [ 50%] Linking CXX executable VMTK_TOY
[build] /usr/bin/ld: cannot find -litkdouble-conversion
[build] /usr/bin/ld: cannot find -lITKInternalEigen3::Eigen
.....

I am new to CMake language and did research for only a few days. I might have to learn it more.
Thanks,

@tigerhu7, that CMakeLists.txt file is meant to be used with the VMTK changes in ENH: Add ITK and VTK to buildtree VMTKConfig.cmake by RafaelPalomar · Pull Request #449 · vmtk/vmtk · GitHub. In these changes I propose to include the ITK and VTK in the VMTKConfig.cmake file, so when you import VMTK through

find_package(VMTK REQUIRED)
include(${VMTK_USE_FILE})

you are also importing ITK and VTK.