# Building SimpleITK as Shared Libs with System HDF5

**URL:** https://discourse.itk.org/t/building-simpleitk-as-shared-libs-with-system-hdf5/3847
**Category:** Beginner Questions
**Tags:** simpleitk
**Created:** [February 9, 2021, 5:37pm UTC](https://discourse.itk.org/t/building-simpleitk-as-shared-libs-with-system-hdf5/3847 "2021-02-09T17:37:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rds](https://discourse.itk.org/user_avatar/discourse.itk.org/rds/32/1906_2.png) [@rds](https://discourse.itk.org/u/rds)
#### Post date: [February 9, 2021, 5:37pm UTC](https://discourse.itk.org/t/building-simpleitk-as-shared-libs-with-system-hdf5/3847/1 "2021-02-09T17:37:52Z")

</div>

Hello,

I am trying to build SimpleITK ( branch master ) with a system version of HDF5 ( v1.10.5 )  
I cloned ITK ( on branch master ), and successfully built it while linking to the system HDF5.

When I try to configure/generate SimpleITK with the system ITK and system HDF5, I get the following error:

```
 CMake Error at Code/IO/src/CMakeLists.txt:21 (add_library):
   Target "SimpleITKIO" links to target "hdf5::hdf5_cpp" but the target was
   not found. Perhaps a find_package() call is missing for an IMPORTED
   target, or an ALIAS target is missing?

 CMake Error at Code/IO/src/CMakeLists.txt:21 (add_library):
   Target "SimpleITKIO" links to target "hdf5::hdf5" but the target was not
   found. Perhaps a find_package() call is missing for an IMPORTED target, or
   an ALIAS target is missing?

```

I have used RelWithDebInfo symbols for HDF5, ITK, and SimpleITK in case that makes a difference.  
I am running Centos7 , with gcc 7.3.1

I’ve searched for a few days now, and am unable to get past this. Any help is greatly appreciated.

---

<div class="post-metadata">

### Author: ![rds](https://discourse.itk.org/user_avatar/discourse.itk.org/rds/32/1906_2.png) [@rds](https://discourse.itk.org/u/rds)
#### Post date: [February 10, 2021, 2:30am UTC](https://discourse.itk.org/t/building-simpleitk-as-shared-libs-with-system-hdf5/3847/2 "2021-02-10T02:30:53Z")

</div>

Using the fix from [COMP: Disable GTest using threads by blowekamp · Pull Request #1503 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/pull/1503)  
I was able to move past this and install SimpleITK with BUILD\_SHARED\_LIBS ON to an install path outside of the build tree.

I added the find\_package(Threads) and gtest\_disable\_pthreads ON to the ITK and SimpleITK CMakeLists.txt. It allows me to get past the Threads::Threads error. More details below.

Please advise if there is a better way to do this.

Using :

- CMake v3.19.4
- HDF5 1.10.5
- ITK v5.1.2
- SimpleITK v2.0.2
- SWIG 4.1.0
- LUA 5.3.5

> 1. Cloned ITK ( v5.1.2 ) and modified the ITK/CMakeLists.txt with :  
> #----------------------------------------------------------------------  
> Generate ITKConfig.cmake for the build tree.  
> +find\_package(Threads)  
> +set(gtest\_disable\_pthreads ON CACHE INTERNAL “”)

and installed with the following cmake config:

```
* CMAKE_BUILD_TYPE RELEASE
* CMAKE_SKIP_INSTALL_RPATH ON
* CMAKE_SKIP_RPATH ON
* Module_ITKIOTransformMINC ON
* Module_ITKReview ON
* Module_SimpleITKFilters ON
* Module_SimpleITKFilters_GIT_TA a2101c137d2c99cf32b2d9600b1e4c3e14722a16
* ITK_USE_SYSTEM_LIBRARIES ON
* ITK_USE_SYSTEM_HDF5 ON
* CMAKE_PREFIX_PATH /opt/ITK-prefix

```

> 1. Install SWIG and LUA. I used versions 4.1.0, and 5.3.5 respectively

> 1. Cloned SimpleITK( v2.0.2 ) and modified the CMakeLists.txt :

installed with the following :

```
* CMAKE_BUILD_TYPE Release
* CMAKE_INSTALL_PREFIX /opt/sitk-prefix
* ITK_DIR /opt/ITK-prefix/lib/cmake/ITK-5.1
* CMAKE_SKIP_INSTALL_RPATH ON
* CMAKE_SKIP_RPATH ON
* SWIG_DIR /opt/swig
* SWIG_EXECUTABLE /opt/swing/bin
* SimpleITK_LUA_EXECUTABLE /opt/lua/bin/lua

```

I was then able to dynamically link these libraries to my app.

( Additional: if having hdf5 linking issues the following helped [https://gitlab.kitware.com/cmake/cmake/-/issues/18560#note\_881956](https://gitlab.kitware.com/cmake/cmake/-/issues/18560#note_881956)  
[BUG: Fix BUILD\_SHARED\_LIBS state consistency for HDF5 by kenavolic · Pull Request #1696 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/pull/1696#issuecomment-624791166) )

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [February 10, 2021, 12:10pm UTC](https://discourse.itk.org/t/building-simpleitk-as-shared-libs-with-system-hdf5/3847/3 "2021-02-10T12:10:59Z")

</div>

Great job tracking down the problem and following up with posting the solution 👏

Have you checked building if the ITK patch is still needed if you use ITK 5.2rc1?

If you are installing SimpleITK into the system, then it frequently makes since to have `CMAKE_SKIP_INSTALL_RPATH=ON`.

The initial post reports the error with `hdf5::hdf5` missing, but the follow up mentions `Threads::Threads` and GTest. Have you enabled testing when building SimpleITK?

I suspect that the system HDF5 is not correctly exporting the transitive “Threads::Threads” dependency.

---

<div class="post-metadata">

### Author: ![rds](https://discourse.itk.org/user_avatar/discourse.itk.org/rds/32/1906_2.png) [@rds](https://discourse.itk.org/u/rds)
#### Post date: [February 10, 2021, 7:26pm UTC](https://discourse.itk.org/t/building-simpleitk-as-shared-libs-with-system-hdf5/3847/4 "2021-02-10T19:26:38Z")

</div>

Thanks, thought it might help someone down the road.

I will give ITK 5.2rc1 a shot, as these build instructions will be needed by other devs on my project, and in our CICD system. Hopefully it works as ideally, I don’t want to make any changes to a cloned library, other than it’s CMake config.

No, I had BUILD\_TESTING OFF.

Though you bring up a good point about the transitive “Threads::Threads” dependency. We had some issues installing HDF5 1.10.5 with CMake correctly on different platforms (Centos, Ubuntu). We had to install with` ./configure` for the HDF5 installation to work on both platforms.

The HDF5 CMake information wasn’t clear at the time, but I may need to look into it again. Especially with the updated targets with newer versions of CMake.

---

<div class="post-metadata">

### Author: ![rds](https://discourse.itk.org/user_avatar/discourse.itk.org/rds/32/1906_2.png) [@rds](https://discourse.itk.org/u/rds)
#### Post date: [February 11, 2021, 4:19pm UTC](https://discourse.itk.org/t/building-simpleitk-as-shared-libs-with-system-hdf5/3847/5 "2021-02-11T16:19:24Z")

</div>

Hi @blowekamp ,

I checked out `ITKv5.2rc1`, and using `SimpleITK v2.0.2` but unfortunately I still get the CMake `Threads::Threads` error when generating.

A second issue that requires manual intervention in the build process. At the end of the SimpleITK build I get these errors:

```
/sandbox/sitkITKrc2/Code/BasicFilters/include/SimpleITKBasicFiltersGeneratedHeaders.i:51: Error: Unable to find 'sitkCoherenceEnhancingDiffusionImageFilter.h'
/sandbox/sitkITKrc2/Code/BasicFilters/include/SimpleITKBasicFiltersGeneratedHeaders.i:152: Error: Unable to find 'sitkLabelSetDilateImageFilter.h'
/sandbox/sitkITKrc2/Code/BasicFilters/include/SimpleITKBasicFiltersGeneratedHeaders.i:153: Error: Unable to find 'sitkLabelSetErodeImageFilter.h'
make[2]: *** [Wrapping/Tcl/CMakeFiles/SimpleITKTclsh.dir/build.make:425: Wrapping/Tcl/SimpleITKTCL_wrap.cxx] Error 1
make[1]: *** [CMakeFiles/Makefile2:1948: Wrapping/Tcl/CMakeFiles/SimpleITKTclsh.dir/all] Error 2
make: *** [Makefile:171: all] Error 2

```

For some reason the generated headers required for the SimpleITK build includes the following.

```
In file: SimpleITK-build/Code/BasicFilters/include/SimpleITKBasicFiltersGeneratedHeaders.i

%include "sitkCoherenceEnhancingDiffusionImageFilter.h"
%include "sitkLabelSetDilateImageFilter.h"
%include "sitkLabelSetErodeImageFilter.h"

```

From my understanding these headers are generated from the ITK Modules `Module_AnisotropicDiffusionLBR` and `Module_LabelErodeDilate` which are not turned `ON` by default in the SuperBuild. So I didn’t enable it when building a non-SuperBuild ITK.

These are the modules I enabled in ITK:

```
Module_SimpleITKFilters ON
Module_ITKIOTransformMINC ON
Module_ITKReview ON

```

When I compare against the SuperBuild’s SimpleITK-build directroy those headers are not included in the `SimpleITKBasicFiltersGeneratedHeaders.i`.

So, I just deleted the `%include ` lines in `SimpleITKBasicFiltersGeneratedHeaders.i` and the non-SuperBuild ran to completion. Allowing me to use the libraries in my app.
