# Including VTK9 built from source in a wider project

**URL:** https://discourse.vtk.org/t/including-vtk9-built-from-source-in-a-wider-project/9612
**Category:** Support
**Created:** [October 14, 2022, 1:25pm UTC](https://discourse.vtk.org/t/including-vtk9-built-from-source-in-a-wider-project/9612 "2022-10-14T13:25:40Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![thomas-wright](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@thomas-wright](https://discourse.vtk.org/u/thomas-wright)
#### Post date: [October 14, 2022, 1:25pm UTC](https://discourse.vtk.org/t/including-vtk9-built-from-source-in-a-wider-project/9612/1 "2022-10-14T13:25:40Z")

</div>

Due to constraints around our build process, I would like to include the VTK source code alongside our own, for our application build configured with cmake. With VTK8 and earlier, we have been using the cmake command `ADD_SUBDIRECTORY` to do this after setting the cmake properties for VTK we need.

However, with VTK9, I am finding a problematic difference between the cmake output produced when doing a standalone VTK build (i.e., configuring VTK independently with cmake and then creating an installation), and the cmake output produced when using `ADD_SUBDIRECTORY`.

In particular, when trying to link against VTK modules using `FIND_PACKAGE(VTK, ...)`, ultimately a file `VTK-targets.cmake` is required, which has not been produced by the configuration. With the standalone build, cmake creates this file in the `lib/cmake/vtk-9.2` directory.

Presumably I am missing a crucial piece of configuration when trying to use VTK within my existing build - please can someone offer insight into what that is? Thank you in advance.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [October 17, 2022, 1:27am UTC](https://discourse.vtk.org/t/including-vtk9-built-from-source-in-a-wider-project/9612/2 "2022-10-17T01:27:43Z")

</div>

Hi @thomas-wright

> `ADD_SUBDIRECTORY`

Well, it is not supported by VTK. Actually, it never was, but it “just worked” before. @ben.boeckel made sure that it does not work anymore to avoid having to support yet another usecase for VTK.

FIND\_PACKAGE is the way to go.

> ultimately a file `VTK-targets.cmake` is required, which has not been produced by the configuration.

This sounds like the issue to fix. Just build and install VTK and you are good.

Best,

---

<div class="post-metadata">

### Author: ![thomas-wright](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@thomas-wright](https://discourse.vtk.org/u/thomas-wright)
#### Post date: [October 17, 2022, 7:53am UTC](https://discourse.vtk.org/t/including-vtk9-built-from-source-in-a-wider-project/9612/3 "2022-10-17T07:53:26Z")

</div>

@mwestphal Thank you for your reply, at least that explains why I can’t get it to work!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [October 17, 2022, 12:12pm UTC](https://discourse.vtk.org/t/including-vtk9-built-from-source-in-a-wider-project/9612/4 "2022-10-17T12:12:11Z")

</div>

Note that there is _a_ way to vendor VTK which is the way that ParaView does it. `add_subdirectory` won’t work as that assumes it is the top-level project (I suppose we could detect that and error…). This involves scanning and building VTK from the parent directory (basically replacing VTK’s top-level `CMakeLists.txt`). The set of things you need to do evolves over time and doesn’t always have explicit docs. You can see what ParaView does when it builds VTK by setting `VTK_`-prefixed variables from `PARAVIEW_`-prefixed ones at least.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [October 23, 2022, 3:28am UTC](https://discourse.vtk.org/t/including-vtk9-built-from-source-in-a-wider-project/9612/5 "2022-10-23T03:28:41Z")

</div>

> [@mwestphal](#):
>
> This sounds like the issue to fix. Just build and install VTK and you are good.

It is important to note that you can fully automate this using CMake’s `ExternalProject_Add` command. Developers just need to build your project and your project downloads, configures, and builds VTK (and all other dependencies).

A complex but very comprehensive example (with building a large number of external dependencies, setting up Python-wrapping, packaging, etc.) is 3D Slicer’s build system. See for example how it configures and builds VTK [here](https://github.com/Slicer/Slicer/blob/main/SuperBuild/External_VTK.cmake).

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [October 24, 2022, 2:23am UTC](https://discourse.vtk.org/t/including-vtk9-built-from-source-in-a-wider-project/9612/6 "2022-10-24T02:23:56Z")

</div>

A different example (and simpler IMO) is the F3D superbuild, which is using an architecture very mush similar to the ParaView superbuild to build VTK and a few other dependencies:

> **[GitHub - f3d-app/f3d-superbuild](https://github.com/f3d-app/f3d-superbuild/)**
>
> Contribute to f3d-app/f3d-superbuild development by creating an account on GitHub.
