hdf5 icon indicating copy to clipboard operation
hdf5 copied to clipboard

Error when trying to use CMake `FetchContent`

Open fnrizzi opened this issue 1 year ago • 8 comments

Describe the bug using FetchContent_Declare and FetchContent_MakeAvailable works fine until i have to link the target, giving me an error

CMake Error at CMakeLists.txt:19 (target_link_libraries):
  Target "cpptest" 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.

Platform (please complete the following information)

  • same errors happens on mac and ubuntu
  • using cmake version 3.29.4

Additional context The full code is attached in the zip

Archive.zip

fnrizzi avatar Jul 04 '24 12:07 fnrizzi

First, this would be a better topic for the HDF5 Forum.

Second it is hard to determine the issue without seeing some code and settings. Is it the intention to build a custom hdf5 library inside another project?

byrnHDF avatar Jul 05 '24 12:07 byrnHDF

Thanks for responding. I was not not aware of the forum.

I actually attached the code in the issue above.

Yes the intention was to try using hdf5 via fetch content.

fnrizzi avatar Jul 05 '24 12:07 fnrizzi

Thank you, I have actually done this with the HDF5Examples, so I know it works. However it was with 1.14.4 release because of issues with CMake settings and var names.

byrnHDF avatar Jul 05 '24 13:07 byrnHDF

Added changed macros in HDF5Examples.cmake file in the HDF5Examples/config/cmake folder:

` macro (EXTERNAL_HDF5_LIBRARY compress_type) set (HDF5_VERSION "1.15.0") set (HDF5_VERSION_MAJOR "1.15") set (HDF5LIB_TGZ_NAME "hdf5.tar.gz" CACHE STRING "Use HDF5LIB from compressed file" FORCE) set (HDF5LIB_TGZ_ORIGPATH "https://github.com/HDFGroup/hdf5/releases/download/snapshot" CACHE STRING "Use HDF5LIB from original location" FORCE) set (HDF5LIB_USE_LOCALCONTENT ON CACHE BOOL "Use local file for HDF5LIB FetchContent" FORCE) set (HDF5LIB_GIT_URL "https://github.com/HDFGroup/hdf5.git" CACHE STRING "Use HDF5LIB from GitHub repository" FORCE) set (HDF5LIB_GIT_BRANCH "develop" CACHE STRING "" FORCE) if (NOT HDF5LIB_USE_LOCALCONTENT) set (HDF5LIB_URL ${H5EXAMPLES_SOURCE_DIR}/${HDF5LIB_TGZ_NAME}) else () set (HDF5LIB_URL ${HDF5LIB_TGZ_ORIGPATH}/${HDF5LIB_TGZ_NAME}) endif () message (VERBOSE "Library HDF5LIB file is ${HDF5LIB_URL}") if (${compress_type} MATCHES "GIT") FetchContent_Declare (HDF5LIB GIT_REPOSITORY ${HDF5LIB_GIT_URL} GIT_TAG ${HDF5LIB_GIT_BRANCH} ) elseif (${compress_type} MATCHES "TGZ") message (VERBOSE "Library HDF5LIB file ${HDF5LIB_URL}") FetchContent_Declare (HDF5LIB URL ${HDF5LIB_URL} URL_HASH "" ) endif () FetchContent_GetProperties(HDF5LIB) if(NOT hdf5lib_POPULATED) FetchContent_Populate(HDF5LIB)

set (BUILD_SHARED_LIBS OFF CACHE BOOL "Build Shared Libraries" FORCE)
set (HDF5_BUILD_CPP_LIB OFF CACHE BOOL "Build C++ support" FORCE)
set (HDF5_BUILD_FORTRAN OFF CACHE BOOL "Build FORTRAN support" FORCE)
set (HDF5_BUILD_JAVA OFF CACHE BOOL "Build JAVA support" FORCE)
set (BUILD_TESTING OFF CACHE BOOL "Build JHDF5 Unit Testing" FORCE)
set (HDF5_BUILD_EXAMPLES OFF CACHE BOOL "Build JHDF5 Library Examples" FORCE)
set (HDF5_BUILD_HL_LIB OFF CACHE BOOL "Build JHDF5 HIGH Level HDF5 Library" FORCE)
set (HDF5_ENABLE_Z_LIB_SUPPORT OFF CACHE BOOL "Enable Zlib Filters" FORCE)
set (HDF5_ENABLE_SZIP_SUPPORT OFF CACHE BOOL "Use SZip Filter" FORCE)

add_subdirectory(${hdf5lib_SOURCE_DIR} ${hdf5lib_BINARY_DIR})

endif()

add_library(${HDF_PACKAGE_NAMESPACE}hdf5lib-static ALIAS hdf5-static) set (H5LIB_STATIC_LIBRARY "${HDF_PACKAGE_NAMESPACE}hdf5lib-static") set (H5LIB_LIBRARIES ${H5LIB_STATIC_LIBRARY}) set (H5LIB_TOOLS ${hdf5lib_BINARY_DIR}/bin})

set (H5LIB_INCLUDE_DIR_GEN "${hdf5lib_BINARY_DIR}/src") set (H5LIB_INCLUDE_DIR "${hdf5lib_SOURCE_DIR}/src") set (H5LIB_FOUND 1) set (H5LIB_INCLUDE_DIRS ${H5LIB_INCLUDE_DIR_GEN} ${H5LIB_INCLUDE_DIR}) message (STATUS "HDF5-${HDF5_VERSION} found: INC=${H5LIB_INCLUDE_DIRS} TOOLS=${H5LIB_TOOLS}") endmacro ()

macro (HDF5_SUPPORT) set (CMAKE_MODULE_PATH ${H5EX_RESOURCES_DIR} ${CMAKE_MODULE_PATH}) option (USE_SHARED_LIBS "Use Shared Libraries" ON)

if (HDF_FETCH_HDF5) include (FetchContent) EXTERNAL_HDF5_LIBRARY(GIT) set (H5EX_HDF5_INCLUDE_DIRS ${H5LIB_INCLUDE_DIRS}) set (H5EX_HDF5_HAVE_H5PUBCONF_H 1) set (H5EX_HDF5_HAVE_HDF5 1) set (H5EX_HDF5_HEADER "h5pubconf.h") set (H5EX_HDF5_LINK_LIBS ${H5EX_HDF5_LINK_LIBS} ${H5LIB_LIBRARIES}) if (NOT TARGET h5dump) add_executable (h5dump IMPORTED) endif() set (H5EX_HDF5_DUMP_EXECUTABLE $<TARGET_FILE:h5dump>) else () `

byrnHDF avatar Jul 05 '24 13:07 byrnHDF

Your welcome to see the full code (note there are a lot of changes with syncing the master code); https://github.com/byrnHDF/hdf5-examples/tree/master-add-hdf5-dir

byrnHDF avatar Jul 05 '24 13:07 byrnHDF

Thanks for this! So are you saying it should work but I am using the wrong version ?

fnrizzi avatar Jul 05 '24 13:07 fnrizzi

I believe that is the situation - we had reports of add_subdirectory failing and I know I fixed that issue at some time in 1.14 development. However, I am not sure if it was fixed for any of the releases. I know it is fixed in the 1_14 branch and develop. As I just verified with the pre-mentioned code macros.

byrnHDF avatar Jul 05 '24 13:07 byrnHDF

Thanks for the help and info on this!

fnrizzi avatar Jul 05 '24 15:07 fnrizzi