问题
I have already installed HDF5 under Ubuntu :
sudo apt install libhdf5-dev
I have a Qt program using HDF5 that compiles fine under CentOS 7 but not under Ubuntu :
erreur : hdf5.h: No such file or directory #include ^~~~~~~~
I am using CMake to generate the build files and in it, I didn't need to handle HDF5 for the CentOS build.
I added this part that I found on the web to the CMake script but I still have a compile error.
FIND_PACKAGE(ZLIB)
FIND_LIBRARY(HDF5_LIBRARY hdf5 ...)
FIND_LIBRARY(HDF5_HL_LIBRARY hdf5_hl ...)
IF(NOT TARGET hdf5 AND NOT TARGET hdf5_hl)
ADD_LIBRARY(hdf5 SHARED IMPORTED)
ADD_LIBRARY(hdf5_hl SHARED IMPORTED)
ENDIF()
SET_TARGET_PROPERTIES(hdf5 PROPERTIES IMPORTED_LOCATION ${HDF5_LIBRARY})
SET_TARGET_PROPERTIES(hdf5_hl PROPERTIES IMPORTED_LOCATION
${HDF5_HL_LIBRARY})
SET(HDF5_LIBRARIES hdf5 hdf5_hl ${ZLIB_LIBRARIES} m)
What can I add to the script to fix the include problem ?
with "locate", I found different hdf5.h :
/usr/include/hdf5/mpich/hdf5.h
/usr/include/hdf5/openmpi/hdf5.h
/usr/include/hdf5/serial/hdf5.h
I really want to know what "libhdf5-dev" refers to ? and why HDF5 is not provided correctly under Ubuntu (it's crazy !)
回答1:
As can be seen in the documentation of imported targets, you also have to populate INTERFACE_INCLUDE_DIRECTORIES to specify headers location. You may want to add the dependency for HL as well with INTERFACE_LINK_LIBRARIES.
So that would be something like:
SET_TARGET_PROPERTIES(hdf5 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${HDF5_INCLUDE_DIRS})
I think this is also the one that you want to specify properly:
SET_TARGET_PROPERTIES(hdf5 PROPERTIES INTERFACE_LINK_LIBRARIES ${ZLIB_LIBRARIES} m)
SET_TARGET_PROPERTIES(hdf5_hl PROPERTIES INTERFACE_LINK_LIBRARIES hdf5)
Instead of your HDF5_LIBRARIES change.
回答2:
Under Ubuntu, I was forced to add MPI to the CMake script. I don't like this solution but its CMake and Ubuntu fault...
find_package(HDF5)
find_package(MPI)
....
target_link_libraries(${binary} ${MPI_LIBRARIES} ${HDF5_LIBRARIES} ... )
target_include_directories(${binary} PRIVATE ${HDF5_INCLUDE_DIRS})
there's also this question : Using serial HDF5 C++ with CMake
libhdf5-dev is, in fact, the serial version https://packages.ubuntu.com/xenial/amd64/libhdf5-dev/filelist
来源:https://stackoverflow.com/questions/54370647/hdf5-h-no-such-file-or-directory-under-ubuntu-and-cmake