Static linking HDF5 library in windows

Hello everyone,

I would like to link the hdf5 library statically, however, I am unable to do it.
I have tried to use the precompiled files, to build the source in debug mode, release mode, using CMake, without using CMake. Nothing works, I always have a list of warnings and errors that begins with

out\build\x64-Debug\LINK : warning LNK4217: symbol ‘isalpha’ defined in ‘libucrt.lib(_ctype.obj)’ is imported by ‘libhdf5_D.lib(H5.obj)’ in function ‘H5_debug_mask’,
and ends with
out\build\x64-Debug\libhdf5_D.lib(H5Clog_trace.obj) : error LNK2001: unresolved external symbol __imp_setbuf
out\build\x64-Debug\libhdf5_D.lib(H5Defl.obj) : error LNK2019: unresolved external symbol __imp__access referenced in function H5D__efl_write
with lots of unresolved symbols in between (Using the release file gives slightly different errors, generally replacing _D with nothing).

My CMake file is comprised of

find_package requires lowcase static or shared

set (LIB_TYPE STATIC) #or SHARED
string(TOLOWER ${LIB_TYPE} SEARCH_TYPE)

find_package( HDF5 NAMES hdf5 COMPONENTS C NO_MODULE REQUIRED ${SEARCH_TYPE} )

stores the library path into a variable called hdf5_lib

set (HDF5_LIB ${HDF5_C_${LIB_TYPE}_LIBRARY})

copy required dlls

file( COPY “${CMAKE_SOURCE_DIR}/lib/HDF5-1.12.0-win64/bin/hdf5.dll” DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

configure library

add_library(Hdf5Interface STATIC
hdf5Interface.cpp
)
target_link_libraries( Hdf5Interface PUBLIC ${HDF5_LIB})
If I replace STATIC to SHARED and uncomment the copy line everything works (dynamically). So, can someone help me figuring out what I am doing wrong?

Hi, I’m facing the same problem. Did you manage to resolve your issue?

Seems to me you are mixing static and shared - You request static HDF5 library and then try to copy dlls which are shared runtime library.

Static builds also require linking with all the dependent libs of hdf5 (szip/zlib). You are only trying to use the hdf5 library - CMake does handle this. Looks like we could do better in our USING_HDF5_CMake.txt file explanation about targets and that you do need to set the variable (environment or CMake) HDF5_ROOT to the install location.

In the HDF5Examples project included in the binaries, there should be a macro HDF5_SUPPORT. This should have a very detailed process for finding the HDF5 library and how to use the variables for building examples.

Allen