# Cannot open module file 'hdf5.mod' (CMake edition)

**URL:** <https://forum.hdfgroup.org/t/cannot-open-module-file-hdf5-mod-cmake-edition/10433>\
**Category:** HDF5 Library\
**Created:** [November 7, 2022, 1:02pm UTC](https://forum.hdfgroup.org/t/cannot-open-module-file-hdf5-mod-cmake-edition/10433 "2022-11-07T13:02:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![carroll.ian](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/carroll.ian/32/1740_2.png) [@carroll.ian](https://forum.hdfgroup.org/u/carroll.ian)\
**Post date:** [November 7, 2022, 1:02pm UTC](https://forum.hdfgroup.org/t/cannot-open-module-file-hdf5-mod-cmake-edition/10433/1 "2022-11-07T13:02:59Z")

</div>

I know this topic has been seen before, but I cannot find a solution in prior posts.

I built HDF5 from CMake-hdf5-1.12.2.tar.gz uncompressed at `<path>`, following instructions to enable Fortran and running `<path>/build-unix.sh`.

I installed by executing `<path>HDF5-1.12.2-Darwin.sh` from ~/Applications. Here is part of the relevant resulting tree

```auto
~/Applications/HDF_Group
└── HDF5
    └── 1.12.2
        ├── bin
        ├── cmake
        │ ├── hdf5-config.cmake
        │ ├── ...
        ├── include
        │ ├── shared
        │ │ ├── hdf5.mod
        │ │ ├── ...
        │ ├── static
        │ │ ├── hdf5.mod
        │ │ ├── ...
        │ ├── ...
        ├── lib
        └── share

```

My project’s CMakeLists.txt includes `find_package(HDF5 REQUIRED COMPONENTS Fortran CONFIG)`. I get no errors with `cmake ../` from the build directory, but `cmake --build .` gives this error:

```auto
   45 | USE HDF5
      | 1
Fatal Error: Cannot open module file 'hdf5.mod' for reading at (1): No such file or directory
compilation terminated.

```

The hdf5.mod file is certainly present, and CMake finds the hdf5-config.cmake file, so I don’t know what to try or what’s going on.

---

<div class="post-metadata">

**Author:** ![byrn](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/b/85f322/32.png) [@byrn](https://forum.hdfgroup.org/u/byrn)\
**Post date:** [November 7, 2022, 2:17pm UTC](https://forum.hdfgroup.org/t/cannot-open-module-file-hdf5-mod-cmake-edition/10433/2 "2022-11-07T14:17:35Z")

</div>

We do test our binaries with the HDF5 Examples project (included in the CMake-hdf5-1.12.2.zipe file).  
In the base CMakeLists.txt file after finding HDF5, we check for Fortran support:

> ```
> option (HDF_BUILD_FORTRAN "Build FORTRAN support" OFF)
> if (HDF_BUILD_FORTRAN AND HDF5_BUILD_FORTRAN)
> set (LINK_Fortran_LIBS ${LINK_LIBS})
> 
> # Parallel IO usage requires MPI to be Linked and Included
> if (H5_HAVE_PARALLEL)
> set (LINK_Fortran_LIBS ${LINK_Fortran_LIBS} ${MPI_Fortran_LIBRARIES})
> if (MPI_Fortran_LINK_FLAGS)
> set (CMAKE_Fortran_EXE_LINKER_FLAGS "${MPI_Fortran_LINK_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")
> endif ()
> endif ()
> 
> add_subdirectory (FORTRAN)
> configure_file (${HDF5EX_F90_SRC_DIR}/H5D/h5_version.h.in ${PROJECT_BINARY_DIR}/FORTRAN/H5D/h5_version.h @ONLY)
> endif ()
> 
> ```

and then in the Fortran subdir (these includes should be moved to target includes, though):

> #-----------------------------------------------------------------------------
> 
> # Setup include Directories
> 
> #-----------------------------------------------------------------------------  
> INCLUDE\_DIRECTORIES (  
> ${CMAKE\_Fortran\_MODULE\_DIRECTORY}  
> ${PROJECT\_BINARY\_DIR}  
> ${CMAKE\_LIBRARY\_OUTPUT\_DIRECTORY}  
> )

then the add\_executable block:

> foreach (example ${examples})  
> get\_filename\_component (example\_name ${example} NAME\_WE)  
> add\_executable (${EXAMPLE\_VARNAME}_f90_${example\_name} ${PROJECT\_SOURCE\_DIR}/${example})  
> target\_compile\_options(${EXAMPLE\_VARNAME}_f90_${example\_name}  
> PRIVATE  
> “-DH5\_LIBVER\_DIR=${H5\_LIBVER\_DIR}”  
> )  
> if (H5\_HAVE\_PARALLEL)  
> target\_include\_directories (${EXAMPLE\_VARNAME}_f90_${example\_name} PUBLIC ${MPI\_Fortran\_INCLUDE\_DIRS})  
> endif ()  
> target\_link\_libraries (${EXAMPLE\_VARNAME}_f90_${example\_name} ${LINK\_Fortran\_LIBS})  
> set\_target\_properties (${EXAMPLE\_VARNAME}_f90_${example\_name} PROPERTIES LINKER\_LANGUAGE Fortran)  
> endforeach ()

Hopefully that helps you.

---

<div class="post-metadata">

**Author:** ![carroll.ian](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/carroll.ian/32/1740_2.png) [@carroll.ian](https://forum.hdfgroup.org/u/carroll.ian)\
**Post date:** [November 7, 2022, 3:44pm UTC](https://forum.hdfgroup.org/t/cannot-open-module-file-hdf5-mod-cmake-edition/10433/3 "2022-11-07T15:44:55Z")

</div>

Sigh, nevermind.

My mistake was:

```auto
target_link_libraries(target hdf5_fortran-shared)

```

When the correct invocation is:

```auto
target_link_libraries(target hdf5::hdf5_fortran-shared)

```

---

<div class="post-metadata">

**Author:** ![carroll.ian](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/carroll.ian/32/1740_2.png) [@carroll.ian](https://forum.hdfgroup.org/u/carroll.ian)\
**Post date:** [December 8, 2022, 9:17pm UTC](https://forum.hdfgroup.org/t/cannot-open-module-file-hdf5-mod-cmake-edition/10433/4 "2022-12-08T21:17:34Z")

</div>

For anyone else struggling with using/enabling/testing Fortran libs with the CMake build, here’s another hint. The magical (and as far as I can tell undocumented) incantation you need to build the Fortran examples is:

```auto
$ cd HDF5Examples
$ mkdir build
$ cd build
$ cmake -DHDF_BUILD_FORTRAN=ON -DBUILD_SHARED_LIBS=ON ../

```

I did not touch any configuration about static vs shared libs before, and it seems to me like shared is the default 🤷‍♂️.
