HDF5 CMake build

We strongly recommend using CMake to build HDF5 rather than autotools. For those building HDF5 on Windows, CMake is required. There is a slight quirk with current (HDF5 1.10.7, 1.12.0) HDF5 CMake scripts in that the “new” cmake -B build flags don’t work properly–the CMake build will fail partway through with weird platform-dependent errors. HDF Group is working to improve CMake and has merged pull requests from us with CMake improvements for HDF5.

These CMake options build HDF5 for Fortran as fast as possible by omitting optional items. If these seems too complicated, try our one-step Python build HDF5 script.

mkdir build
cd build

cmake -S /path/to/hdf5_source -DCMAKE_INSTALL_PREFIX=~/.local/hdf5 -DHDF5_GENERATE_HEADERS:BOOL=false -DHDF5_DISABLE_COMPILER_WARNINGS:BOOL=true -DBUILD_SHARED_LIBS:BOOL=false -DCMAKE_BUILD_TYPE=Release -DHDF5_BUILD_FORTRAN:BOOL=true -DHDF5_BUILD_CPP_LIB:BOOL=false -DHDF5_BUILD_TOOLS:BOOL=false -DBUILD_TESTING:BOOL=false -DHDF5_BUILD_EXAMPLES:BOOL=false

This creates static and dynamic HDF5 libraries under the user install prefix–we don’t show the library suffixes for simplicity. Note: the *stub files may not be present.

hdf5/lib/libhdf5
hdf5/lib/libhdf5_fortran
hdf5/lib/libhdf5_hl
hdf5/lib/libhdf5_hl_fortran
hdf5/lib/libhdf5hl_fortran
hdf5/lib/libhdf5_hl_f90cstub
hdf5/lib/libhdf5_f90cstub

The Fortran .mod files that need to be included are under

hdf5/include/

Usage

Tell CMake to use this HDF5 from your project by:

cmake -B build -DHDF5_ROOT=~/.local/hdf5

cmake --build build