Tools have static linking when built with CMake

I recently discovered that the HDF5 tools prefer to be linked with the static HDF5 library when building HDF5 with CMake on Linux. This differ from the configure default behavior.

If I do not specify anything special, both CMake and configure build both static and shared libraries. This is fine. But the tools like h5diff, h5dump etc. link against the static libraries when built with CMake, and against the shared ones when built with configure.

If I set the -DONLY_SHARED_LIBS=ON when running CMake, then only dynamic libraries are built, and the tools of course link against the dynamic libraries… But then the tools are renamed from “h5diff” to “h5diff-shared”!!! Why???

The effect in file size between the two options are dramatic - when using dynamic linking h5diff is 215 kB and h5dump is 443 kB - and when using static linking they are 16 MB each. I pack HDF5 into a redistribute package where size matters and the HDF5 tools alone are ~220 MB!

I do not need the static libraries for any particular purpose - so I am happy setting -DONLY_SHARED_LIBS=ON. But the fact that the tools are renamed and get a -shared extension is very cumbersome. I would manually need to rename them or make some symlinks in there for this to work.

This behavior is sort of documented in the bottom of release_docs/RELEASE.txt.

I still wonder about the rationale for these decisions. I like CMake because it is usually very good in picking up compiler flags, link paths etc. It has supported building HDF5 with the new LLVM-based Intel compilers (icx, ifx) for a long time and way before these compilers could be used with the configure script. HDF5 1.14.0 still only builds with these compilers using CMake, not configure, so configure is not really an option.

Could there be any options for building these tools in the same way across both configureand CMake? Why is the suffix -shared needed? Is there any workaround besides manually renaming the binaries after “make install”?

I appreciate all comments. Thanks.

1 Like

An update from my side:

I tried to do a file rename on these tools after having built and installed the HDF5 library with CMake:

cd $HDF5_ROOT/bin
for i in *-shared; do
    mv $i ${i%-shared}
done

That worked - the tools have their expected names that we all know and expect. However - when I want to consume the HDF5 library in a downstream application I am building with CMake, I get the following:

CMake Error at /opt/hdf5/1.14.0/install/cmake/hdf5-targets.cmake:168 (message):
  The imported target "h5diff-shared" references the file

     "/opt/hdf5/1.14.0/install/bin/h5diff-shared"

  but this file does not exist.  Possible reasons include:

  * The file was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and contained

     "/opt/hdf5/1.14.0/install/cmake/hdf5-targets.cmake"

  but not all the files it references.

Call Stack (most recent call first):
  /opt/hdf5/1.14.0/install/cmake/hdf5-config.cmake:145 (include)
  /usr/local/share/cmake-3.28/Modules/FindHDF5.cmake:510 (find_package)
  CMakeLists.txt:23 (find_package)

so that option did not go well.

I guess I now need to check if making a symlink “h5diff-shared” point to the “h5diff” binary will “fool” CMake or if I will need to use some sed string replacement magic on these binary names…

It seems this is now solved in a released version (1.14.4). Probably by this PR: Make sure that ONLY_SHARED_LIBS option is correctly set by byrnHDF · Pull Request #2544 · HDFGroup/hdf5 · GitHub