Example of using FetchContent to bring HDF5 ?

Dear all,

I’m looking for a working example of using FetchContent to bring HDF5 (and its CPP wrapper) into a larger CMake build.

I’m confused by what exactly I have to define prior to calling FetchContent and, more trivially, what kind of target names are supposed to be defined…

This is what I’ve tried :

include(FetchContent)

set(HDF5_ENABLE_SZIP_SUPPORT OFF CACHE INTERNAL "")
set(HDF5_ENABLE_SZIP_ENCODING OFF CACHE INTERNAL "")
set(HDF5_BUILD_EXAMPLES OFF CACHE INTERNAL "")
set(HDF5_BUILD_CPP_LIB ON CACHE INTERNAL "")
set(HDF5_ENABLE_Z_LIB_SUPPORT OFF CACHE INTERNAL "")
set(HDF5_EXTERNALLY_CONFIGURED 1 CACHE INTERNAL "")
set(HDF5_NAMESPACE hdf5::)
FetchContent_Declare(HDF5
  URL https://github.com/HDFGroup/hdf5/archive/refs/tags/hdf5-1_14_3.tar.gz
  URL_HASH SHA256=df5ee33c74d5efb59738075ef96f4201588e1f1eeb233f047ac7fd1072dee1f6 
  DOWNLOAD_EXTRACT_TIMESTAMP FALSE
  FIND_PACKAGE_ARGS)
FetchContent_MakeAvailable(HDF5)

But during configure phase I get a target was not found, clearly indicating I’m misunderstanding how the target ought to be named…

CMake Error at utils/CMakeLists.txt:9 (target_link_libraries):
  Target "toashort-to-h5" links to:

    hdf5::hdf5

  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.

Thanks,

Take a look at the HDF5 source for the config/cmake/HDFLibMacros.cmake file where we use FetchContent_* to compile in the zlib/libaec libs.
Note that FetchContent_MakeAvailable call is a combo of the

if(NOT hdf5_zlib_POPULATED)
    FetchContent_Populate

but after that if block, notice the add_library and set commands; these would likely be equivalent to the ALIAS reason.