Skip to content

Error when check_hdf5_feature #3321

@PierreChatelier

Description

@PierreChatelier

I had a hard time trying to run CMake with NetCDF with a dependent HDF5.
It kept claiming that my HDF5 was not compiled with ZLIB support - which is wrong
I tracked down the problem up to the check trying to detect H5_HAVE_ZLIB_H

My HDF5 does have #define H5_HAVE_ZLIB_H 1
but the CMake of NetCDF was not detecting it at configure stage.

I finally found the problem in the test of check_hdf5.cmake

function(check_hdf5_feature VAR FEATURE)
   if (NOT _H5_FEATURE_HEADER)
     check_hdf5_feature_header()
   endif()
 
   include(CheckCSourceCompiles)
   set(CMAKE_REQUIRED_INCLUDES ${HDF5_INCLUDE_DIR})
 
   message(STATUS "Checking for ${FEATURE}")

   check_c_source_compiles("
 #include <${_H5_FEATURE_HEADER}>
 #if !${FEATURE}
 #error
 #endif
 int main() {}"
     _has_${FEATURE})
 
   set(${VAR} ${_has_${FEATURE}} PARENT_SCOPE)
 endfunction()
 

The problem are the lines

 #if !${FEATURE}
 #error
 #endif

I think they are interpreted as a comment of CMake, not as C code !
It must be escaped properly.

The best fix I think is using the [=[...]=] syntax

 function(check_hdf5_feature VAR FEATURE)
   if (NOT _H5_FEATURE_HEADER)
     check_hdf5_feature_header()
   endif()
 
   include(CheckCSourceCompiles)
   set(CMAKE_REQUIRED_INCLUDES ${HDF5_INCLUDE_DIR})
 
   message(STATUS "Checking for ${FEATURE}")

   check_c_source_compiles([=[
 #include <${_H5_FEATURE_HEADER}>
 #if !${FEATURE}
 #error
 #endif
 int main() {}]=]
     _has_${FEATURE})
 
   set(${VAR} ${_has_${FEATURE}} PARENT_SCOPE)
 endfunction()
 

Also, I had to add
unset(_has_${FEATURE} CACHE)

because of cache poisoning during my tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions