HDF5 CMake build

We strongly recommend using CMake to build HDF5. CMake is required to build HDF5 on Windows. HDF Group continues to work to improve HDF5 CMake scripts, but here are a couple bugs present at least through HDF5 1.12.0 and 1.10.7:

  • cmake -S hdf5 -B hdf5/build flags don’t work properly–the CMake build will fail partway through with weird platform-dependent errors. Instead you need to use old-fashioned mkdir hdf5/build; cd hdf5/build; cmake ..
  • HDF5 builds test executables that don’t pickup CMake environment variables. You must set compiler variables CC and FC in the shell. This is particularly true when using non-system-default compilers.

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 hdf5/build
cd hdf5/build

cmake .. -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

cmake --build . --parallel

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/

Tell CMake to use this HDF5 from your project by:

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