# Use C preprocessor macros (e.g. version numbers) in Fortran

**URL:** <https://forum.hdfgroup.org/t/use-c-preprocessor-macros-e-g-version-numbers-in-fortran/7298>\
**Category:** HDF5 Library\
**Created:** [July 17, 2020, 1:14pm UTC](https://forum.hdfgroup.org/t/use-c-preprocessor-macros-e-g-version-numbers-in-fortran/7298 "2020-07-17T13:14:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan.schilling](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/jonathan.schilling/32/1214_2.png) [@jonathan.schilling](https://forum.hdfgroup.org/u/jonathan.schilling)\
**Post date:** [July 17, 2020, 1:14pm UTC](https://forum.hdfgroup.org/t/use-c-preprocessor-macros-e-g-version-numbers-in-fortran/7298/1 "2020-07-17T13:14:19Z")

</div>

Hi there,

I am currently working on a Fortran codebase, which uses HDF5 as output file format. The code runs on various machines, from laptops to supercomputers (hence many different compilers, versions of HDF5 etc.). The build system currently in use is `make`.

In the Fortran code to read the files ouput by the names code, I use `h5lget_info_f` for querying if a Dataset is present in a file or not.  
The API of `h5lget_info_f`changed from HDF5-1.10 to HDF5-1.12. My current approach is to use the C preprocessor on the Fortran sources to only enable the call applicable to the HDF5 version in use. However, in the Fortran sources, the version number preprocessor macros defined in `H5public.h` are not availabe and I cannot simply include `H5public.h` into the Fortran sources, since it also contains some C source code. So, I wrote a small C program (get\_hdf5\_version; you can find it on [https://github.com/jonathanschilling/get\_hdf5\_version](https://github.com/jonathanschilling/get_hdf5_version)) to print the HDF5 version numbers. I use this program to define the preprocessor macros `H5_VERS_MAJOR` etc. in the Makefile.

First question: Is there a more elegant way of solving this issue? Probably we should simply migrate to cmake for a build system, but this is currently not an option.

Second question: Would you like to integrate get\_hdf5\_version (probably renamed to h5version or similar) into the HDF5 releases? As you can see, it does not have any external dependencies, so it should be relatively easy to do this. Of course this only makes sense if you back-integrate it also into HDF5 versions 1.8 and 1.10 and make new releases there, which then include the utility.

Best regards,  
Jonathan Schilling

---

<div class="post-metadata">

**Author:** ![koziol](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/k/4da419/32.png) [@koziol](https://forum.hdfgroup.org/u/koziol)\
**Post date:** [July 17, 2020, 5:13pm UTC](https://forum.hdfgroup.org/t/use-c-preprocessor-macros-e-g-version-numbers-in-fortran/7298/2 "2020-07-17T17:13:19Z")

</div>

FYI - H5Lexists may be better than H5Lget\_info for checking if an object/link exists.

---

<div class="post-metadata">

**Author:** ![jonathan.schilling](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/jonathan.schilling/32/1214_2.png) [@jonathan.schilling](https://forum.hdfgroup.org/u/jonathan.schilling)\
**Post date:** [July 20, 2020, 10:06am UTC](https://forum.hdfgroup.org/t/use-c-preprocessor-macros-e-g-version-numbers-in-fortran/7298/3 "2020-07-20T10:06:44Z")

</div>

@koziol Thanks for the hint. I can use this method for now.

@all: How about the remaining issue of using preprocessor macros (e.g. for version numbers) within other programming languages than C?

---

<div class="post-metadata">

**Author:** ![furutaka.kazuyoshi](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/f/e5b9ba/32.png) [@furutaka.kazuyoshi](https://forum.hdfgroup.org/u/furutaka.kazuyoshi)\
**Post date:** [June 19, 2026, 2:51am UTC](https://forum.hdfgroup.org/t/use-c-preprocessor-macros-e-g-version-numbers-in-fortran/7298/4 "2026-06-19T02:51:04Z")

</div>

Any updates on this matter???

---

<div class="post-metadata">

**Author:** ![brtnfld](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/brtnfld/32/1444_2.png) [@brtnfld](https://forum.hdfgroup.org/u/brtnfld)\
**Post date:** [June 19, 2026, 4:02am UTC](https://forum.hdfgroup.org/t/use-c-preprocessor-macros-e-g-version-numbers-in-fortran/7298/5 "2026-06-19T04:02:45Z")

</div>

Which aspect are you referring to?

As of HDF5 2.0.0, HDF5 generates and installs `H5config_f.inc`. It contains _only_ `#define`s (no C code), so you can `#include` it directly from any `.F90` source. It also contains other `#define`s in addition to the VERSION macros.

---

<div class="post-metadata">

**Author:** ![furutaka.kazuyoshi](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/f/e5b9ba/32.png) [@furutaka.kazuyoshi](https://forum.hdfgroup.org/u/furutaka.kazuyoshi)\
**Post date:** [June 19, 2026, 4:33am UTC](https://forum.hdfgroup.org/t/use-c-preprocessor-macros-e-g-version-numbers-in-fortran/7298/6 "2026-06-19T04:33:32Z")

</div>

Thanks for the info. I wanted to know the macros on version numbers.

(Are there any ways for the older versions of the library?)

Kazuyoshi

---

<div class="post-metadata">

**Author:** ![brtnfld](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/brtnfld/32/1444_2.png) [@brtnfld](https://forum.hdfgroup.org/u/brtnfld)\
**Post date:** [June 19, 2026, 2:16pm UTC](https://forum.hdfgroup.org/t/use-c-preprocessor-macros-e-g-version-numbers-in-fortran/7298/7 "2026-06-19T14:16:22Z")

</div>

The version macros got back ported to 1.14.4, but not to anything older. For older versions, and if you are using cmake to build your application, you can define your own:

```auto
find_package(HDF5 REQUIRED COMPONENTS Fortran)

if(HDF5_VERSION VERSION_GREATER_EQUAL "1.10" AND HDF5_VERSION VERSION_LESS "1.12")
  # this is the 1.10.x series specifically
  target_compile_definitions(myapp PRIVATE HDF5_IS_1_10=1)
endif()

```
