opencv_contrib
opencv_contrib copied to clipboard
Statically linking to HDF5 library raises linker errors
System information (version)
- OpenCV => 4.8.0
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2022
Detailed description
We recently tried to compile OpenCV's HDF module, which only works by default while linking to a shared HDF5 libary. However, as it's a single-module dependendy, statically linking to HDF5 is advantageous here. Still, this results in linker errors caused by hardcoded configuration options for shared library linking. In particular, the flags inside the Windows branch in the CMakeLists.txt defined by
add_definitions(-DH5_BUILT_AS_DYNAMIC_LIB -D_HDF5USEDLL_)
result in linker errors. After removing them, only a linker error from the missing ShLwApi.Lib is raised.
These two issues can be resolved by extending above line to
if(HDF5_C_LIBRARY MATCHES "libhdf5.lib$")
set(HDF5_LIBRARIES "${HDF5_LIBRARIES};ShLwApi.Lib")
else()
add_definitions(-DH5_BUILT_AS_DYNAMIC_LIB -D_HDF5USEDLL_)
endif()
and compiling works with both, shared and static library linking to HDF5. Still, we wanted to discuss this as an issue first before creating a respective PR.
Steps to reproduce
After cloning OpenCV's main and contrib repositories, run CMake with the following flags:
OPENCV_EXTRA_MODULES_PATHpointing to the contrib'smodulesdirectoryHDF5_INCLUDE_DIRSpointing to theincludedirectory inside the HDF5 path (generated by its CMake install)HDF5_C_LIBRARYpointing to the staticlib/libhdf5.libin the same path
Now, building of the HDF module raises a bunch of linker error.
Issue submission checklist
- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [ ] There is reproducer code and related data files: videos, images, onnx, etc