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

**URL:** <https://discourse.slicer.org/t/problem-with-using-cmakelists-to-run-vmtk-c-code/27936>\
**Category:** VMTK\
**Created:** [February 20, 2023, 4:10pm UTC](https://discourse.slicer.org/t/problem-with-using-cmakelists-to-run-vmtk-c-code/27936 "2023-02-20T16:10:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tigerhu7](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tigerhu7/32/67201_2.png) [@tigerhu7](https://discourse.slicer.org/u/tigerhu7)\
**Post date:** [February 20, 2023, 4:10pm UTC](https://discourse.slicer.org/t/problem-with-using-cmakelists-to-run-vmtk-c-code/27936/1 "2023-02-20T16:10:18Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/2/32665e11b77c215fa062a94b296e6b9220f531a3.png)  
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

 ![Screenshot from 2023-02-20 23-35-30-mod](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/0/20bf7dedda2941fb0894f0b9eb5d694cf28d081b.png)

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

 ![Screenshot from 2023-02-20 23-48-04-mod](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/f/af93c357068cf00c360dc34f1fb5efea8b8166e6.png)

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/8/e8ddb0e826c8e3654db3b505d06c86baac5e45e8.png)  
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;  
> }

---

<div class="post-metadata">

**Author:** ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)\
**Post date:** [February 21, 2023, 9:36am UTC](https://discourse.slicer.org/t/problem-with-using-cmakelists-to-run-vmtk-c-code/27936/2 "2023-02-21T09:36:27Z")

</div>

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](https://github.com/vmtk/vmtk/issues/448)) and a PR ([ENH: Add ITK and VTK to buildtree VMTKConfig.cmake by RafaelPalomar · Pull Request #449 · vmtk/vmtk · GitHub](https://github.com/vmtk/vmtk/pull/449)). 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:

```auto
 # 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})

```

```plaintext
// 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.

---

<div class="post-metadata">

**Author:** ![tigerhu7](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tigerhu7/32/67201_2.png) [@tigerhu7](https://discourse.slicer.org/u/tigerhu7)\
**Post date:** [February 21, 2023, 1:12pm UTC](https://discourse.slicer.org/t/problem-with-using-cmakelists-to-run-vmtk-c-code/27936/3 "2023-02-21T13:12:28Z")

</div>

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

```auto
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!!

---

<div class="post-metadata">

**Author:** ![tigerhu7](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tigerhu7/32/67201_2.png) [@tigerhu7](https://discourse.slicer.org/u/tigerhu7)\
**Post date:** [February 22, 2023, 11:33am UTC](https://discourse.slicer.org/t/problem-with-using-cmakelists-to-run-vmtk-c-code/27936/4 "2023-02-22T11:33:49Z")

</div>

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

```auto
 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

```auto
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.

```auto
[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,

---

<div class="post-metadata">

**Author:** ![RafaelPalomar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rafaelpalomar/32/1436_2.png) [@RafaelPalomar](https://discourse.slicer.org/u/RafaelPalomar)\
**Post date:** [February 22, 2023, 12:58pm UTC](https://discourse.slicer.org/t/problem-with-using-cmakelists-to-run-vmtk-c-code/27936/5 "2023-02-22T12:58:28Z")

</div>

> [@tigerhu7](#):
>
> I have tested your CMakeLists.txt file, unfortunately it did not work for me.  
> If I use yours, I got this error

@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](https://github.com/vmtk/vmtk/pull/449). In these changes I propose to include the ITK and VTK in the `VMTKConfig.cmake` file, so when you import VMTK through

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

```

you are also importing ITK and VTK.
