Using HDF5 in C, C++, Fortran

It’s easy and fast to write streaming unlimited size HDF5 files from high level scripted languages such as Matlab and Python. It is also relatively straightforward to do so from lower level languages such as C, C++ and Fortran.

Examples: C/C++/Fortran with HDF5

A simple program invokes the compiler directly, by something like

cc myprog.c func.c -lm

Adding HDF5 capability requires a slightly modified compile command using the macro h5cc

HDF group provides a script h5cc linking the needed HDF5 libraries upon installing libhdf5-dev, so do

h5cc myprog.c func.c -lm

Likewise, there is h5c++ for C++ and h5fc for Fortran. You are not required to use these scripts, and in larger programs linking numerous files and libraries you can easily include HDF5 in your CMakeLists.txt file.

Leave a Comment