I am trying to compile an application with the HDF5 lib. I installed the lib via ubuntus 18.04 package manager. My CMakeLists looks like
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(hdf)
find_package(HDF5 REQUIRED COMPONENTS C CXX)
add_executable(hdf hdf.cpp)
target_link_libraries(hdf ${HDF5_HL_LIBRARIES} ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})
set_property(TARGET hdf PROPERTY CXX_STANDARD 17)
message(STATUS "INCLUDE LOCATION" ${HDF5_INCLUDE_DIRS})
message(STATUS "version" ${HDF5_VERSION})
message(STATUS "DEFINITIONS" ${HDF5_DEFINITIONS})
message(STATUS "LIBRARIES" ${HDF5_LIBRARIES})
message(STATUS "HL_LIBRARIES" ${HDF5_HL_LIBRARIES})
running cmake, the output yields
HDF5: Using hdf5 compiler wrapper to determine C configuration
-- HDF5: Using hdf5 compiler wrapper to determine CXX configuration
-- Found HDF5: /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;/usr/lib/x86_64-linux-gnu/libz.so;/usr/lib/x86_64-linux-gnu/libdl.so;/usr/lib/x86_64-linux-gnu/libm.so (found version "1.10.0.1") found components: C CXX
-- INCLUDE LOCATION/usr/include/hdf5/serial
-- version1.10.0.1
-- DEFINITIONS-D_FORTIFY_SOURCE=2
-- 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/usr/lib/x86_64-linux-gnu/libz.so/usr/lib/x86_64-linux-gnu/libdl.so/usr/lib/x86_64-linux-gnu/libm.so
-- HL_LIBRARIES
so apparently the files are all found.
However, if I am not trying to compile a simple example and include the dependencies with
#include "H5Cpp.h"
I get
fatal error: H5Cpp.h: No such file or directory
#include "H5Cpp.h"
why is that? Help is appreciated
In versions of cmake older than 3.19, the targets exposed by the ${HDF5_LIBRARIES} do not automatically set the include directories.
You need to explicitly set:
target_include_directories(hdf PRIVATE ${HDF5_INCLUDE_DIRS})
As of cmake 3.19, find_package(HDF5)
creates proper targets you can use for target_link_libraries
which will automatically set the correct includes:
target_link_libraries(hdf PRIVATE hdf5::hdf5 hdf5::hdf5_cpp)
Compare https://cmake.org/cmake/help/latest/module/FindHDF5.html (current) to https://cmake.org/cmake/help/v3.18/module/FindHDF5.html (last version before the proper targets were introducted).
A version that would support both new and old cmake would be:
find_package(HDF5 REQUIRED COMPONENTS C CXX)
if(TARGET hdf5::hdf5)
# cmake >= 3.19
target_link_libraries(hdf PRIVATE hdf5::hdf5 hdf5::hdf5_cpp)
else()
# cmake < 3.19
target_link_libraries(hdf PRIVATE ${HDF5_C_LIBRARIES} ${HDF5_CXX_LIBRARIES})
target_include_directories(hdf PRIVATE ${HDF5_C_INCLUDE_DIRS} ${HDF5_CXX_INCLUDE_DIRS}
endif()