Static libraries with CMake on Ubuntu


Hello everybody,

i want to use hdf5 with static libraries in a simple cmake C++ project and cant get it to work. I use the the Ubuntu package libhdf5-dev. I googled a lot and didnt find anything useful. I am no expert in neither Ubuntu, nor cmake nor C++, so I am glad for any help.

I will describe my problem in detail and also give some feedback from my experience and point of view. So either I can learn the errors of my ways or it might help future development of Hdf.

My Setup

I use CLion as IDE on Windows 10. It is connected to a WSL Ubuntu 18.04.3 LTS where I use GCC. I installed the package libhdf5-dev via sudo apt-get install libhdf5-dev.

apt show libhdf5-dev gets me:

Package: libhdf5-dev
Version: 1.10.0-patch1+docs-4
Priority: optional
Section: universe/libdevel
Source: hdf5
Origin: Ubuntu
Maintainer: Ubuntu Developers
Original-Maintainer: Debian GIS Project
Installed-Size: 11.6 MB
Provides: libhdf5-serial-dev
Depends: libhdf5-100 (= 1.10.0-patch1+docs-4), zlib1g-dev, libjpeg-dev, libaec-dev, hdf5-helpers, libhdf5-cpp-100 (= 1.10.0-patch1+docs-4)
Suggests: libhdf5-doc
Breaks: libhdf5-serial-dev (<< 1.8.12-9~)
Replaces: libhdf5-serial-dev (<< 1.8.12-9~)
Supported: 3y
Download-Size: 2461 kB
APT-Manual-Installed: no
APT-Sources: bionic/universe amd64 Packages
Description: Hierarchical Data Format 5 (HDF5) - development files - serial version
HDF5 is a file format and library for storing scientific data.
HDF5 was designed and implemented to address the deficiencies of
HDF4.x. It has a more powerful and flexible data model, supports
files larger than 2 GB, and supports parallel I/O.
This package contains development files for serial platforms.

Question: Using packages in Ubuntu is usually best practice if you dont have a really good reason for compiling a library or program by yourself. The existence of the package is nowhere mentioned on the whole hdf website. Is there any reason? If not this should be fixed immediately. Lots of explanations on the hdf website are very confusing and only seem to apply for Windows or some special cases.


I compiled a program on said Ubuntu 18.04 (with shared hdf5 library) and wanted it to run on another PC with Ubuntu 20.04. Here I installed hdf with sudo apt-get install libhdf5-dev too. It turns out there is no compatibility with the hdf5 libraries on both systems.

On the Ubuntu 20.04 there are libhdf5-103 and libhdf5-cpp-103 versions. I read a fourm post somewhere and created symbolic links but this didnt work. I found no solution to either install the 103 versions on 18.04 or the 100 versions on 20.04

Thus, I proceeded and wanted to use hdf5 with static libraries which is more desirable for me anyways.

Question: Both Ubuntu 18.04 and 20.04 are LTS versions which are under active maintenance. Why is the 18.04 package no longer maintained, as it seems? Why is there no downwards compatibility or at least a possibility to use the 100-versions on 20.04?

My Problem

I cant get cmake to use static hdf libraries. The following CMakeLists works for me but only for shared libraries:

cmake_minimum_required(VERSION 3.10.2)
add_executable(h5staticTest main.cpp)

I have CMake 3.10.2 installed. The Building hdf with cmake help says that 3.12 is required and 3.15 is recommended. But I dont want to build it myself. At another point somewhere it is said The HDF5 1.10.x product requires a minimum CMake version of 3.10.1. So I assume the cmake version is not the problem.

The Build and Install HDF5 Applications with CMake help mentions the HDF5_DIR environment variable:

Set the environment variable HDF5_DIR to the installed location of
the config files for HDF5. On Windows:
HDF5_DIR=C:/Program Files/HDF_Group/HDF5/1.10.x/cmake

Question: How do I need to set HDF5_DIR on Ubuntu? Is it needed for the packaged hdf version at all? A solution like this with an environent variable seems very odd to me in combination with cmake. Any explanations and examples for Linux and the corresponding package are missing.

In the hdf help mentionend above a minimal example is given:

cmake_minimum_required (VERSION 3.10.1)
project (HDF5MyApp C CXX)


find_package (HDF5 NAMES hdf5 COMPONENTS C ${SEARCH_TYPE})
#find_package (HDF5) # Find non-cmake built HDF5

set (example hdf_example)

add_executable (${example} ${PROJECT_SOURCE_DIR}/${example}.c)
TARGET_C_PROPERTIES (${example} ${LIB_TYPE} " " " ")
target_link_libraries (${example} ${LINK_LIBS})

My CMake doesnt know the command TARGET_C_PROPERTIES. I tried the above example without this line and it doesnt work.

Question: I have a pure C++ program. Where are the changes in the above code between C and C++? The differences about all the different apis on the hdf website are very confusing.

The variable set(HDF5_USE_STATIC_LIBRARIES ON) which I stumbled on while googleing does nothing in any context I tried.

A command like FIND_PACKAGE(HDF5 COMPONENTS C CXX NO_MODULE REQUIRED static) does not work (probably because of HDF5_DIR not set).

Long story short

Can somebody please help me create a very simple CMakeLists with static libraries with the Ubuntu package? It seems to me like a really basic thing actually…

Some explanations and answers to my questions would be nice too.


Here is a link to a cmake setup, I use in my examples, peruse it for ideas.

hope it helps: steve