c++ 错误:H5Cpp.h:没有这样的文件或目录

1l5u6lss  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(226)

我试图用HDF5库编译一个应用程序。我通过ubuntus 18.04包管理器安装了这个库。我的CMakeLists看起来像

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})

字符串
运行cmake,输出将产生

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


显然文件都找到了
但是,如果我不是试图编译一个简单的示例,

#include "H5Cpp.h"


我得到

fatal error: H5Cpp.h: No such file or directory
 #include "H5Cpp.h"


为什么?很感谢你的帮助

kse8i1jr

kse8i1jr1#

在早于3.19的cmake版本中,${HDF5_LIBRARIES}公开的目标不会自动设置包含目录。
您需要显式设置:

target_include_directories(hdf PRIVATE ${HDF5_INCLUDE_DIRS})

字符串
从cmake 3.19开始,find_package(HDF5)创建了可以用于target_link_libraries的适当目标,它将自动设置正确的包含:

target_link_libraries(hdf PRIVATE hdf5::hdf5 hdf5::hdf5_cpp)


https://cmake.org/cmake/help/latest/module/FindHDF5.html(当前)与https://cmake.org/cmake/help/v3.18/module/FindHDF5.html(引入适当目标之前的最后一个版本)进行比较。
一个同时支持新旧cmake的版本是:

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()

相关问题