# vtkAddon with VTK 8.2.0 build fails with C++17

**URL:** <https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512>\
**Category:** Development\
**Created:** [May 7, 2021, 6:56pm UTC](https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512 "2021-05-07T18:56:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [May 7, 2021, 6:56pm UTC](https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512/1 "2021-05-07T18:56:29Z")

</div>

Hi,

VTK 8.2.0 in `VTK/Common/Core/vtkMath.h` needs to have the following include:

```cpp
#include <algorithm> // for std::clamp

```

aimed to support `C++17` (and higher).

With MSVC I used to successfully compile Slicer with C++17 but now I’m working on Ubuntu and the build fails with error (GCC 9.3):

```cpp
In file included from /vtkAddon/vtkOrientedBSplineTransform.cxx:13:
/VTK/Common/Core/vtkMath.h: In static member function ‘static T vtkMath::ClampValue(const T&, const T&, const T&)’:
/VTK/Common/Core/vtkMath.h:1516:15: error: ‘clamp’ is not a member of ‘std’
 1516 | return std::clamp(value, min, max);
      | ^ ~~~~
make[2]: *** [CMakeFiles/vtkAddon.dir/build.make:121: CMakeFiles/vtkAddon.dir/vtkOrientedBSplineTransform.cxx.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/vtkAddon.dir/all] Error 2
make: *** [Makefile:149: all] Error 2

```

By the way [VTK 9 has fixed this](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/Core/vtkMath.h#L51)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 7, 2021, 8:26pm UTC](https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512/2 "2021-05-07T20:26:53Z")

</div>

Thanks for reporting this. In Slicer, we don’t support building VTK8.2 with C++17 (C++17 is disabled in Slicer superbuild), so they do not have to be compatible, but the change you suggest is harmless, so if you submit a pull request to vtkAddon then we’ll merge it.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [May 7, 2021, 8:36pm UTC](https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512/3 "2021-05-07T20:36:44Z")

</div>

In best scenario I should push a PR to VTK 8.2.0 as `#include <algorithm>` should normally reside there.  
Less accurate decision is probably to put `#include <algorithm>` before including `vtkMath.h` in `vtkAddon`.

I don’t know (I’m not much experienced git user) is it possible to push PR to VTK 8.2.0 (or is it worth it?)?  
If not I will try to push PR to `vtkAddon`

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [May 8, 2021, 11:54am UTC](https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512/4 "2021-05-08T11:54:06Z")

</div>

I decided to [make PR to vtkAddon](https://github.com/Slicer/vtkAddon/pull/21)

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [May 8, 2021, 12:05pm UTC](https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512/5 "2021-05-08T12:05:39Z")

</div>

Reading [VTK 8.2 does not compile with c++17 (#17615) · Issues · VTK / VTK · GitLab](https://gitlab.kitware.com/vtk/vtk/-/issues/17615), it appears that VTK 8.2 never was explicitly supporting C++17.

Is there a reason you are not building Slicer with VTK9 where it does not have this issue with C++17? The Slicer preview builds are already using VTK9 well. If you are building a Slicer custom application or building Slicer from source you would just need to set the VTK version to use as “9” instead of the default “8”.

@lassoan Should the default Slicer build option for VTK be switched to “9” now? Most VTK9 related issues have been resolved and since it is used in the Slicer preview packages now it is likely worthy of being the default CMake option?

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [May 8, 2021, 2:16pm UTC](https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512/6 "2021-05-08T14:16:14Z")

</div>

Hi,

Thank you for information.

> [@jamesobutler](#):
>
> Is there a reason you are not building Slicer with VTK9 where it does not have this issue with C++17?

Actually I would prefer VTK 9, but when I’m trying to force VTK 9 in SlicerCAT with the line-code in my CMakeLists.txt:

```bash
set(Slicer_VTK_VERSION_MAJOR 9) # or set(Slicer_VTK_VERSION_MAJOR:STRING "9")

```

I can see that Slicer still builds VTK 8.2.0. So for now I just tried to fix VTK 8.2.0 bug and in the near future I will try to investigate why I can’t build it against VTK 9.

I took a first look how `External_VTK.cmake` works:

```bash
  if("${Slicer_VTK_VERSION_MAJOR}" STREQUAL "8")

    set(_git_tag "97904fdcc7e73446b3131f32eac9fc9781b23c2d") # slicer-v8.2.0-2018-10-02-74d9488523

    set(vtk_egg_info_version "8.2.0")

  elseif("${Slicer_VTK_VERSION_MAJOR}" STREQUAL "9")

    set(_git_tag "f3c1a72fbf0f7287575ae876efced9c85776d9b4") # slicer-v9.0.20201111-733234c785

    set(vtk_egg_info_version "9.0.20201111")

  else()

    message(FATAL_ERROR "error: Unsupported Slicer_VTK_VERSION_MAJOR: ${Slicer_VTK_VERSION_MAJOR}")

  endif()

```

and it is quite straghtforward. But as I said SlicerCAT have built VTK 8.2.0 for me.

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [May 8, 2021, 4:24pm UTC](https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512/7 "2021-05-08T16:24:03Z")

</div>

You will need to make sure your SlicerCAT is using the latest Slicer repo git hash and then you can select Slicer\_VTK\_VERSION\_MAJOR as “9” and it should build well. If you are using an earlier Slicer git hash version that could’ve been when VTK9 support wasn’t working appropriately.

You should keep the VTK git hash version that Slicer specified in External\_VTK which is a specific Slicer fork of VTK.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 9, 2021, 3:17pm UTC](https://discourse.slicer.org/t/vtkaddon-with-vtk-8-2-0-build-fails-with-c-17/17512/8 "2021-05-09T15:17:02Z")

</div>

> [@jamesobutler](#):
>
> Should the default Slicer build option for VTK be switched to “9” now? Most VTK9 related issues have been resolved and since it is used in the Slicer preview packages now it is likely worthy of being the default CMake option?

Good point. I’ve submitted a pull request to change the default VTK version to 9:

> <https://github.com/Slicer/Slicer/pull/5633>
>
> Slicer Preview Releases are built with VTK9 and there are no major issues, there…fore we update the default VTK version to 9.
