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

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

~/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:

   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.

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.

Sigh, nevermind.

My mistake was:

target_link_libraries(target hdf5_fortran-shared)

When the correct invocation is:

target_link_libraries(target hdf5::hdf5_fortran-shared)

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:

$ 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 :man_shrugging:.