# Use HDF5 library in a Slicer module

**URL:** <https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877>\
**Category:** Development\
**Created:** [December 4, 2020, 2:11am UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877 "2020-12-04T02:11:38Z")\
**Posts on this page:** 12\
**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:** [December 4, 2020, 2:11am UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/1 "2020-12-04T02:11:38Z")

</div>

Hi,

Is it possible to build Slicer with custom HDF5 library? I mean I would like to build HDF5 separetely from SLicer  
I can see that VTK and ITK somehow use it but there is no `hdf5_D.lib` or `hdf5_hl_D.lib` in Slicer build dir (`VTK-build` and `ITK-build` folders)

For now when I try to use my custom HDF5 lib in my loadable module I get link errors like:  
**h5core.obj:-1: error: LNK2019: unresolved external symbol itk\_H5open referenced in function “public: \_\_cdecl HighFive::AtomicType::AtomicType(void)” (??0?$AtomicType@H@HighFive@@QEAA@XZ) [C:\S\extensions\build-MSV-Qt\_msvc2019\_64-Debug\MSVCore\msvcore.vcxproj]**

I use HighFive C++ wrapper for HDF5

---

<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:** [December 4, 2020, 3:04am UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/2 "2020-12-04T03:04:09Z")

</div>

Slicer does not use HDF5 directly, only via its dependent libraries - ITK and VTK. You should be able to pass down your custom HDF5 library the same way as it is done for zlib (check out \*.cmake files in the SuperBuild folder), but you would only need to do this if you wanted ITK and VTK to use your custom HDF5, which is probably not what you need.

If you want your module to use custom HDF5 then you can build your custom HDF5 along with other modules and libraries in your extension (you can use the superbuild extension template). Both ITK and VTK use name mangling when building HDF, so there will be no conflicts.

I’m just curious, why do you need to use a custom HDF5 library?

---

<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:** [December 4, 2020, 4:03am UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/3 "2020-12-04T04:03:34Z")

</div>

Thank you for explaination

> [@lassoan](#):
>
> I’m just curious, why do you need to use a custom HDF5 library?

I’m sorry I confused you. The word **custom** is inapropriate.  
I need HDF5 library configured with almost any CMake settings. I would like to try to use the same HDF5 library that VTK or ITK uses but I didn’t find HDF5 library with names something like `hdf5_D.lib` in VTK-build and ITK-build subfolders.

I have written some small **core** shared lib that uses HDF5 via [HighFive wrapper header only C++ library](https://github.com/BlueBrain/HighFive). As I’m not going to display this **core** in **Slicer app** and in the same time many of my loadable modules use this **core** , I decided to make this **core** as a subfolder in my extension and **not to make** a module from this **core**. And modules of this extension simply links to this **core**.  
When I build this I get many errors like this:  
**h5core.obj:-1: error: LNK2019: unresolved external symbol itk\_H5open referenced in function “public: \_\_cdecl HighFive::AtomicType::AtomicType(void)” (??0?$AtomicType@H@HighFive@@QEAA@XZ) [C:\S\extensions\build-MSV-Qt\_msvc2019\_64-Debug\MSVCore\msvcore.vcxproj]**

I decided that the problem is that VTK and ITK uses HDF5 libs and I also include **static** `libhdf5_D.lib` and `libhdf5_hl_D.lib`

Well the problem is that I can’t link HDF5 to my **core** shared lib which is subfolder of extension and there is some custom HDF5, probably I would like to use one of the latest HDF5 lib

---

<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:** [December 4, 2020, 5:32am UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/4 "2020-12-04T05:32:31Z")

</div>

> [@keri](#):
>
> modules of this extension simply links to this **core**.  
> When I build this I get many errors like this:  
> **h5core.obj:-1: error: LNK2019: unresolved external symbol itk\_H5open referenced in function “public: \_\_cdecl HighFive::AtomicType::AtomicType(void)” (??0?$AtomicType@H@HighFive@@QEAA@XZ) [C:\S\extensions\build-MSV-Qt\_msvc2019\_64-Debug\MSVCore\msvcore.vcxproj]**

Probably you just need to link ITK’s HDF5 library to your target. In general, if you use ITK’s or VTK’s name-mangled HDF5 then you need to link the corresponding ITK/VTK library.

If ITK/VTK is built so that HDF5 library is not exported as a publicly usable target outside ITK/VTK then you can build any HDF5 library implementation yourself, as part of your extension superbuild, and use that. See for example [SlicerJupyter extension](https://github.com/Slicer/SlicerJupyter/tree/master/SuperBuild), which builds and uses several third-party libraries.

---

<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:** [December 5, 2020, 7:43pm UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/5 "2020-12-05T19:43:02Z")

</div>

Thank you for information.

As I’m studying Slicer I’m trying now to simply build HDF5 lib with SuperBuild. So to do this I guess I should work only with CMake. Is there the simplest example showing how to build third party C++ external library with Slicer superbuild?  
I tried to follow the example you gave with Slicer Jupiter but somehow my code doesn’t launch build step for HDF5 even if in general build is succesfull.

---

<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:** [December 5, 2020, 8:33pm UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/6 "2020-12-05T20:33:57Z")

</div>

SlicerRT extension is a simpler example: [https://github.com/SlicerRt/SlicerRT/tree/master/SuperBuild](https://github.com/SlicerRt/SlicerRT/tree/master/SuperBuild)

---

<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:** [December 6, 2020, 5:35am UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/7 "2020-12-06T05:35:48Z")

</div>

Thank you. I’ve found my mistake. I should have been look more precisely at `ExternalProject_Add(...)` CMake parameters.

Now inside my module the command `find_package(hdf5 REQUIRED)` fails with error:

_CMake Error at C:/S/extensions/Proba-build/HDF5-build/hdf5-config.cmake:25 (message): File or directory C:/S/extensions/bin referenced by variable HDF5\_TOOLS\_DIR does not exist !_

Here _C:/S/extensions/bin_ should be _C:\S\extensions\Proba-build\HDF5-build\tools_ but I can’t understand why it points to wrong directory. If you had some experience of superbuild `HDF5` did you encounter similar error?  
I use `hdf5-1_12_0` git tag, tomorrow I will try some other tag

---

<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:** [December 6, 2020, 5:52am UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/8 "2020-12-06T05:52:51Z")

</div>

If you send a link to your repository then somebody can have a look.

---

<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:** [December 6, 2020, 6:53am UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/9 "2020-12-06T06:53:49Z")

</div>

Here it is the repository: [https://github.com/KerimMatlab/Proba/tree/main](https://github.com/KerimMatlab/Proba/tree/main)  
I’m ashamed that it has strange name. I just started exploring slicer and didnt think that I would create repo from this test extension

---

<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:** [December 6, 2020, 2:33pm UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/10 "2020-12-06T14:33:25Z")

</div>

I’ve had a look. You need to use hdf5 instead of HDF5 (this is how the library calls itself in its CMake config file) and not specify NO\_MODULE when you find the package: [https://github.com/KerimMatlab/Proba/pull/1/files](https://github.com/KerimMatlab/Proba/pull/1/files)

---

<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:** [December 6, 2020, 6:04pm UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/11 "2020-12-06T18:04:23Z")

</div>

Thank you very much!

Now I can compile hdf5 and I’m trying to link this hdf5 to my `KKK` module.  
Like in the Jupiter example I tried to add `hdf5` in:

```
set(MODULE_TARGET_LIBRARIES
  vtkSlicer${MODULE_NAME}ModuleLogic
  qSlicer${MODULE_NAME}ModuleWidgets
  hdf5
  )

```

But I get link error:

`:- 1: error: LNK1104: cannot open file 'hdf5.lib' [C:\S\extensions\Proba_fixed\Proba-build\inner-build\KKK\qSlicerKKKModule.vcxproj] [C:\S\extensions\Proba_fixed\Proba-build\inner.vcxproj]`

Probably this is beacause there no such `hdf5.lib` library (even if I flagged cmake `-DBUILD_SHARED_LIBS=ON` and `-DBUILD_STATIC_LIBS=ON`)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/3/33efb45a1e7e0d03cf4f80fc86cf3606576b4df0.png)

I commited git repository

---

<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:** [December 6, 2020, 8:11pm UTC](https://discourse.slicer.org/t/use-hdf5-library-in-a-slicer-module/14877/12 "2020-12-06T20:11:48Z")

</div>

I ve figured out that this error is the result of the fact that `find_package(hdf5 REQUIRED)` produces the message:

RUNNING KKK  
CMake Warning (dev) at C:/Qt/Tools/CMake\_64/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:424 (message):  
The package name passed to `find_package_handle_standard_args` (HDF5) does  
not match the name of the calling package (hdf5). This can lead to  
problems in calling code that expects `find_package` result variables  
(e.g., `_FOUND`) to follow a certain pattern.  
Call Stack (most recent call first):  
C:/Qt/Tools/CMake\_64/share/cmake-3.19/Modules/FindHDF5.cmake:975 (find\_package\_handle\_standard\_args)  
C:/S/d/VTK/CMake/FindHDF5.cmake:9 (include)  
KKK/CMakeLists.txt:10 (find\_package)  
This warning is for project developers. Use -Wno-dev to suppress it.

– Could NOT find HDF5 (missing: HDF5\_LIBRARIES HDF5\_INCLUDE\_DIRS) (found version “”)

In my code now there no upper case variables `HDF5` only smaller `hdf5`
