Skip to content

[CMake] Use hdf target as opposed to HDF5_CXX_LIBRARIES#1665

Merged
KrisThielemans merged 3 commits intoUCL:masterfrom
KrisThielemans:cmake_hdf5_target
Nov 12, 2025
Merged

[CMake] Use hdf target as opposed to HDF5_CXX_LIBRARIES#1665
KrisThielemans merged 3 commits intoUCL:masterfrom
KrisThielemans:cmake_hdf5_target

Conversation

@KrisThielemans
Copy link
Copy Markdown
Collaborator

Using targets is recommended, as it will add compiler flags etc as needed. It might also resolve
conda-forge/stir-feedstock#149, as with this update, STIRTargets.cmake contains hdf5::hdf5_cpp as opposed to explicit filenames.

This fix was recommended by @traversaro at #general > dependency on $BUILD_PREFIX/...libpthread.so problem @ 💬

Using targets is recommended, as it will add compiler flags etc as needed.
It might also resolve
conda-forge/stir-feedstock#149, as with this update,
`STIRTargets.cmake` contains `hdf5::hdf5_cpp` as opposed to explicit filenames.
@KrisThielemans KrisThielemans added this to the v6.4 milestone Nov 11, 2025
@KrisThielemans KrisThielemans self-assigned this Nov 11, 2025
@KrisThielemans
Copy link
Copy Markdown
Collaborator Author

hmmm. The "demos" that link against STIR have

[435/606] Linking CXX executable src/examples/C++/using_STIR_LOCAL/demo1
FAILED: [code=1] src/examples/C++/using_STIR_LOCAL/demo1 
 && /usr/bin/g++ -O3 -DNDEBUG -ffast-math -Wl,--dependency-file=src/examples/C++/using_STIR_LOCAL/CMakeFiles/demo4_obj_fun.dir/link.d src/CMakeFiles/stir_registries.dir/IO/IO_registries.cxx.o src/CMakeFiles/stir_registries.dir/buildblock/buildblock_registries.cxx.o src/CMakeFiles/stir_registries.dir/recon_buildblock/recon_buildblock_registries.cxx.o src/CMakeFiles/stir_registries.dir/data_buildblock/data_buildblock_registries.cxx.o src/CMakeFiles/stir_registries.dir/Shape_buildblock/Shape_buildblock_registries.cxx.o src/CMakeFiles/stir_registries.dir/modelling_buildblock/modelling_registries.cxx.o src/CMakeFiles/stir_registries.dir/spatial_transformation_buildblock/spatial_transformation_registries.cxx.o src/CMakeFiles/stir_registries.dir/scatter_buildblock/scatter_registries.cxx.o src/examples/C++/using_STIR_LOCAL/CMakeFiles/demo4_obj_fun.dir/demo4_obj_fun.cxx.o -o src/examples/C++/using_STIR_LOCAL/demo4_obj_fun  -Wl,-rpath,/usr/lib/x86_64-linux-gnu/hdf5/serial:/home/runner/work/STIR/STIR/install/lib:  src/analytic/FBP3DRP/libanalytic_FBP3DRP.a  src/analytic/FBP2D/libanalytic_FBP2D.a  src/analytic/SRT2D/libanalytic_SRT2D.a  src/analytic/SRT2DSPECT/libanalytic_SRT2DSPECT.a  src/analytic/GRD2D/libanalytic_GRD2D.a  src/analytic/DDSR2D/libanalytic_DDSR2D.a  src/iterative/OSMAPOSL/libiterative_OSMAPOSL.a  src/iterative/KOSMAPOSL/libiterative_KOSMAPOSL.a  src/iterative/OSSPS/libiterative_OSSPS.a  src/scatter_buildblock/libscatter_buildblock.a  src/Shape_buildblock/libShape_buildblock.a  src/eval_buildblock/libeval_buildblock.a  src/buildblock/libstir_buildblock.a  /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_cpp.so  /home/runner/work/STIR/STIR/install/lib/libparallelproj_c.so.1.7.3 && :
/usr/bin/ld: src/buildblock/libstir_buildblock.a(GEHDF5Wrapper.cxx.o): undefined reference to symbol 'H5check_version@@HDF5_SERIAL_1.8.7'
/usr/bin/ld: /lib/x86_64-linux-gnu/libhdf5_serial.so.103: error adding symbols: DSO missing from command line

I also see this on my machine, including for lm_to_projdata

@KrisThielemans
Copy link
Copy Markdown
Collaborator Author

KrisThielemans commented Nov 11, 2025

Multiple possible causes:

  • When compiling GEHDF5Wrapper.cxx, an hdf5.h incompatible with the rest is found
  • The target is incomplete. In particular, adding libhdf5.so to the linker line makes this problem go away.

On my system, when using a conda-forge hdf5, adding

    cmake_print_properties(
      TARGETS hdf5::hdf5_cpp hdf5::hdf5 HDF5::HDF5
      PROPERTIES LOCATION INTERFACE_INCLUDE_DIRECTORIES IMPORTED_LINK_INTERFACE_LIBRARIES IMPORTED_LINK_DEPENDENT_LIBRARIES LINK_INTERFACE_LIBRARIES
      )

and using -DHDF5_FIND_DEBUG=ON, I see

- HDF5 CXX compiler wrapper is unable to compile a minimal HDF5 program.
-- HDF5_DIR: HDF5_DIR-NOTFOUND
-- HDF5_DEFINITIONS: 
-- HDF5_INCLUDE_DIRS: /home/sirfuser/mambaforge/envs/stirdev/include
-- HDF5_LIBRARIES: /home/sirfuser/mambaforge/envs/stirdev/lib/libhdf5_cpp.so;/home/sirfuser/mambaforge/envs/stirdev/lib/libhdf5.so
-- HDF5_HL_LIBRARIES: 
-- HDF5_CXX_DEFINITIONS: 
-- HDF5_CXX_INCLUDE_DIR: /home/sirfuser/mambaforge/envs/stirdev/include
-- HDF5_CXX_INCLUDE_DIRS: /home/sirfuser/mambaforge/envs/stirdev/include
-- HDF5_CXX_LIBRARY: 
-- HDF5_CXX_LIBRARIES: /home/sirfuser/mambaforge/envs/stirdev/lib/libhdf5_cpp.so;/home/sirfuser/mambaforge/envs/stirdev/lib/libhdf5.so
-- HDF5_CXX_HL_LIBRARY: 
-- HDF5_CXX_HL_LIBRARIES: 
-- Defined targets (if any):
-- ... hdf5::hdf5_cpp
-- 
 Properties for TARGET hdf5::hdf5_cpp:
   hdf5::hdf5_cpp.LOCATION = "/home/sirfuser/mambaforge/envs/stirdev/lib/libhdf5_cpp.so"
   hdf5::hdf5_cpp.INTERFACE_INCLUDE_DIRECTORIES = "/home/sirfuser/mambaforge/envs/stirdev/include"
   hdf5::hdf5_cpp.IMPORTED_LINK_INTERFACE_LIBRARIES = <NOTFOUND>
   hdf5::hdf5_cpp.IMPORTED_LINK_DEPENDENT_LIBRARIES = <NOTFOUND>
   hdf5::hdf5_cpp.LINK_INTERFACE_LIBRARIES = <NOTFOUND>

 No such TARGET "hdf5::hdf5" !

 Properties for TARGET HDF5::HDF5:
   HDF5::HDF5.LOCATION = <NOTFOUND>
   HDF5::HDF5.INTERFACE_INCLUDE_DIRECTORIES = "/home/sirfuser/mambaforge/envs/stirdev/include"
   HDF5::HDF5.IMPORTED_LINK_INTERFACE_LIBRARIES = <NOTFOUND>
   HDF5::HDF5.IMPORTED_LINK_DEPENDENT_LIBRARIES = <NOTFOUND>
   HDF5::HDF5.LINK_INTERFACE_LIBRARIES = <NOTFOUND>

This is the usual case where if there is no HDF5Config.cmake (and there isn't one in conda (WIP: conda-forge/hdf5-feedstock#255) nor APT), and there is no h5c++ (there isn't one in conda-forge unless you also use cxx-compiler), FindHDF5.cmake fails miserably. Usually, it fails because it doesn't know about depending libraries such as libz. Here it seems a bug in FindHDF5.cmake, as HDF5_CXX_LIBRARIES does have libhdf5.so, but the target doesn't.

sigh

@KrisThielemans
Copy link
Copy Markdown
Collaborator Author

KrisThielemans commented Nov 11, 2025

Apologies, I was printing the old LINK_INTERFACE_LIBRARIES as opposed to INTERFACE_LINK_LIBRARIES

Properties for TARGET hdf5::hdf5_cpp:
   hdf5::hdf5_cpp.LOCATION = "/home/sirfuser/mambaforge/envs/stirdev/lib/libhdf5_cpp.so"
   hdf5::hdf5_cpp.INTERFACE_INCLUDE_DIRECTORIES = "/home/sirfuser/mambaforge/envs/stirdev/include"
   hdf5::hdf5_cpp.IMPORTED_LINK_INTERFACE_LIBRARIES = <NOTFOUND>
   hdf5::hdf5_cpp.IMPORTED_LINK_DEPENDENT_LIBRARIES = <NOTFOUND>
   hdf5::hdf5_cpp.INTERFACE_LINK_LIBRARIES = <NOTFOUND>

 No such TARGET "hdf5::hdf5" !

 Properties for TARGET HDF5::HDF5:
   HDF5::HDF5.LOCATION = <NOTFOUND>
   HDF5::HDF5.INTERFACE_INCLUDE_DIRECTORIES = "/home/sirfuser/mambaforge/envs/stirdev/include"
   HDF5::HDF5.IMPORTED_LINK_INTERFACE_LIBRARIES = <NOTFOUND>
   HDF5::HDF5.IMPORTED_LINK_DEPENDENT_LIBRARIES = <NOTFOUND>
   HDF5::HDF5.INTERFACE_LINK_LIBRARIES = "/home/sirfuser/mambaforge/envs/stirdev/lib/libhdf5_cpp.so;/home/sirfuser/mambaforge/envs/stirdev/lib/libhdf5.so"

When using the system (APT) version, and h5c++ installed

-- HDF5: Using hdf5 compiler wrapper to determine CXX configuration
-- HDF5_DIR: HDF5_DIR-NOTFOUND
-- HDF5_DEFINITIONS: -D_FORTIFY_SOURCE=2
-- HDF5_INCLUDE_DIRS: /usr/include/hdf5/serial
-- HDF5_LIBRARIES: /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_cpp.so;/usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/usr/lib/x86_64-linux-gnu/libsz.so;/home/sirfuser/devel/install/lib/libz.a;/usr/lib/x86_64-linux-gnu/libdl.so;/usr/lib/x86_64-linux-gnu/libm.so
-- HDF5_HL_LIBRARIES: 
-- HDF5_CXX_DEFINITIONS: -D_FORTIFY_SOURCE=2
-- HDF5_CXX_INCLUDE_DIR: 
-- HDF5_CXX_INCLUDE_DIRS: /usr/include/hdf5/serial
-- HDF5_CXX_LIBRARY: 
-- HDF5_CXX_LIBRARIES: /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_cpp.so;/usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/usr/lib/x86_64-linux-gnu/libsz.so;/home/sirfuser/devel/install/lib/libz.a;/usr/lib/x86_64-linux-gnu/libdl.so;/usr/lib/x86_64-linux-gnu/libm.so
-- HDF5_CXX_HL_LIBRARY: 
-- HDF5_CXX_HL_LIBRARIES: 
-- Defined targets (if any):
-- ... hdf5::hdf5_cpp
-- 
 Properties for TARGET hdf5::hdf5_cpp:
   hdf5::hdf5_cpp.LOCATION = "/usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_cpp.so"
   hdf5::hdf5_cpp.INTERFACE_INCLUDE_DIRECTORIES = "/usr/include/hdf5/serial"
   hdf5::hdf5_cpp.IMPORTED_LINK_INTERFACE_LIBRARIES = <NOTFOUND>
   hdf5::hdf5_cpp.IMPORTED_LINK_DEPENDENT_LIBRARIES = <NOTFOUND>
   hdf5::hdf5_cpp.INTERFACE_LINK_LIBRARIES = <NOTFOUND>

 No such TARGET "hdf5::hdf5" !

 Properties for TARGET HDF5::HDF5:
   HDF5::HDF5.LOCATION = <NOTFOUND>
   HDF5::HDF5.INTERFACE_INCLUDE_DIRECTORIES = "/usr/include/hdf5/serial"
   HDF5::HDF5.IMPORTED_LINK_INTERFACE_LIBRARIES = <NOTFOUND>
   HDF5::HDF5.IMPORTED_LINK_DEPENDENT_LIBRARIES = <NOTFOUND>
   HDF5::HDF5.INTERFACE_LINK_LIBRARIES = "/usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_cpp.so;/usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5.so;/usr/lib/x86_64-linux-gnu/libpthread.so;/usr/lib/x86_64-linux-gnu/libsz.so;/home/sirfuser/devel/install/lib/libz.a;/usr/lib/x86_64-linux-gnu/libdl.so;/usr/lib/x86_64-linux-gnu/libm.so"

So, it seems we would have more chance with using HDF5::HDF5.

@KrisThielemans
Copy link
Copy Markdown
Collaborator Author

mark it as advanced to avoid confusing users
@KrisThielemans KrisThielemans merged commit 0e667f7 into UCL:master Nov 12, 2025
13 checks passed
@KrisThielemans KrisThielemans deleted the cmake_hdf5_target branch November 12, 2025 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant