A tiny, header-only C++17 wrapper around the HDF5 C++ API that makes common write/read flows painless: scalars, vectors, matrices, and compound datasets. It auto-creates group paths, keeps your data strongly typed, and stays out of your way.
- Simple API for the 80% use case: save/read values, vectors, matrices, and compound records.
- Strong typing with minimal boilerplate.
- Header-only: just include
HDF5.hpp. - Works with vanilla
h5c++orpkg-config.
- Save/read scalars, vectors, matrices, and compound datasets.
- Supports
int,unsigned int,unsigned short,std::size_t,long long,unsigned long long,char,unsigned char,bool,float,double, andstd::string. - Variable-length string support for vectors and matrices.
- Automatic group creation from dataset paths like
"group/subgroup/data". - Clear error reporting via exceptions.
- HDF5 with C++ interface (
libhdf5_cpp). - C++17 compiler.
pkg-config(optional, for automatic flags).
#include <vector>
#include <string>
#include "HDF5.hpp"
int main()
{
HDF5 file("data.h5", HDF5::OpenMode::TRUNC);
int answer = 42;
std::vector<double> samples{0.5, 1.5, 2.5};
std::vector<std::size_t> dims{2, 3};
std::vector<float> matrix{1, 2, 3, 4, 5, 6};
file.saveValue("scalars/answer", answer);
file.saveVector("vectors/samples", samples);
file.saveMatrix("matrices/float", matrix, dims);
return 0;
}make examplesBinaries land in bin/.
./bin/basic
./bin/compoundmake tests
./bin/run_hdf5_testsHDF5 file("data.h5", HDF5::OpenMode::TRUNC);
file.saveValue("path", value);
file.readValue("path", value_out);
file.saveVector("path", vector);
file.readVector("path", vector_out);
file.saveMatrix("path", vector_or_ptr, dims);
file.readMatrix("path", vector_out, dims_out);
file.saveCompound("path", names, vec1, vec2, ...);
file.readCompound("path", names, vec1_out, vec2_out, ...);Open modes:
RDONLY,RDWR,TRUNC,EXCL,CREAT,SWMR_WRITE,SWMR_READ
HDF5.hpp: single header with the full implementation.examples/: minimal sample programs.run_hdf5_tests.cpp: integration tests.Makefile: build targets for examples and tests.
- Datasets are created with fixed dimensions; pass the full
dimsyou want to store. - For compound datasets, all vectors must be the same length and provide matching field names.
If you want a CMake build or extended type support, open an issue or send a PR.