# Using part of VTK in my own project?

**URL:** https://discourse.vtk.org/t/using-part-of-vtk-in-my-own-project/3010
**Category:** Support
**Created:** [April 9, 2020, 9:53pm UTC](https://discourse.vtk.org/t/using-part-of-vtk-in-my-own-project/3010 "2020-04-09T21:53:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ahmed_salih/32/1164_2.png) [@Ahmed\_Salih](https://discourse.vtk.org/u/Ahmed_Salih)
#### Post date: [April 9, 2020, 9:53pm UTC](https://discourse.vtk.org/t/using-part-of-vtk-in-my-own-project/3010/1 "2020-04-09T21:53:36Z")

</div>

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/](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

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ahmed_salih/32/1164_2.png) [@Ahmed\_Salih](https://discourse.vtk.org/u/Ahmed_Salih)
#### Post date: [April 10, 2020, 2:48pm UTC](https://discourse.vtk.org/t/using-part-of-vtk-in-my-own-project/3010/2 "2020-04-10T14:48:23Z")

</div>

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](https://discourse.vtk.org/uploads/default/original/2X/d/de0f92aedb426f865967b8e88b3df81e2d6c484b.png)

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

Kind regards

---

<div class="post-metadata">

### Author: ![danlipsa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/danlipsa/32/390_2.png) [@danlipsa](https://discourse.vtk.org/u/danlipsa)
#### Post date: [April 10, 2020, 3:02pm UTC](https://discourse.vtk.org/t/using-part-of-vtk-in-my-own-project/3010/3 "2020-04-10T15:02:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ahmed_salih/32/1164_2.png) [@Ahmed\_Salih](https://discourse.vtk.org/u/Ahmed_Salih)
#### Post date: [April 10, 2020, 3:14pm UTC](https://discourse.vtk.org/t/using-part-of-vtk-in-my-own-project/3010/4 "2020-04-10T15:14:47Z")

</div>

Sure 🙂 So this is what I have right now:

For the VTK file generation

 ![image](https://discourse.vtk.org/uploads/default/original/2X/5/5ddf93d8a0fc67ea5162132f8685347bef44420d.png)

For the whole project:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/d/dd3e986cced4e6ba5457197668cadb6d0d5a4c10.png)

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

 ![image](https://discourse.vtk.org/uploads/default/original/2X/9/9e7fcb47df979a7ff05874441cf3fc7220b9c3d1.png)

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

Kind regards

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ahmed_salih/32/1164_2.png) [@Ahmed\_Salih](https://discourse.vtk.org/u/Ahmed_Salih)
#### Post date: [April 10, 2020, 3:23pm UTC](https://discourse.vtk.org/t/using-part-of-vtk-in-my-own-project/3010/5 "2020-04-10T15:23:47Z")

</div>

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](https://discourse.vtk.org/uploads/default/original/2X/1/1a77b7ae371d587e05c3242cab95dbd5378fb88b.png)

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:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/8/8069944ef6cbecfedeca750a19bba933f69f87d1.png)

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

Kind regards
