HDFGroup / hdf5

Official HDF5® Library Repository
https://www.hdfgroup.org/
Other
588 stars 243 forks source link

Can not use parallel target in CMake in pure C++ project #3844

Open cmacmackin opened 10 months ago

cmacmackin commented 10 months ago

Describe the bug When trying to link against the parallel HDF5 target in CMake in a C++ project, CMake complains that the MPI_C target is not found:

CMake Error at /home/cmacmack/.spack/opt/spack/linux-linuxmint21-icelake/gcc-11.3.0/hdf5-1.14.1-2-nabfxjrrsno6gmfw5fsnot4b5geztyd3/cmake/hdf5-targets.cmake:68 (set_target_properties):
  The link interface of target "hdf5-shared" contains:

    MPI::MPI_C

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.

This is related to but distinct from the bug addressed by #2400. The reason for the error is that the project only enables the C++ language and CMake therefore does not import the MPI_C target. (Presumably there would be a similar issue in Fortran projects.) Adding enable_language(C) before importing the HDF5 package fixes the problem.

Expected behavior Parallel HDF5 imports the MPI::MPI_C package to satisfy its public dependency.

Platform (please complete the following information)

HDF5 and its dependencies were compiled using Spack v0.20.3.

mirenradia commented 5 months ago

I have the same problem in a pure Fortran project where Fortran is the only enabled language. I'm doing

set(HDF5_PREFER_PARALLEL TRUE)
find_package(HDF5 REQUIRED COMPONENTS Fortran)
if (NOT HDF5_IS_PARALLEL)
    message(FATAL_ERROR "Parallel HDF5 Required.")
endif ()

and my software versions are as follows

HDF5, CMake, OpenMPI and their dependencies were built with Spack at d5c1e16e43.

Is it possible to get around this without enabling C (which slows down CMake configuration and is otherwise unneeded)? This only seems to be a problem with HDF5 1.14 and I've not had this problem with various build of 1.12 and 1.10.