cmake 安装HDF5 for C++

6yoyoihd  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(185)

我试图用HDF5编写一个C++程序,但我在构建过程中遇到了困难。cmake似乎找不到HDF5库。下面是我尝试过的:
main.cpp:

#include <iostream>
#include <hdf5.h>

int main(){
    std::cout << "Hello World" << std::endl;
}

字符串
CMakeLitsts.txt:

project(HelloWorld)
cmake_minimum_required(VERSION 3.27)

add_executable(hello_world main.cpp)

#  what do I have to do here? The find_package(HDF5 REQUIRED) fails as it doesnt find the HDF5 library
include_directories(${HDF5_INCLUDE_DIRS})
find_package(HDF5 REQUIRED)
target_link_libraries( hello_world ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})


运行cmake失败并显示错误消息

Could NOT find HDF5 (missing: HDF5_LIBRARIES HDF5_INCLUDE_DIRS) (found version "")


我在.local/hdf5文件夹中下载并构建HDF5(我使用的是Ubuntu)使用

mkdir build
cd build
cmake ..
make


我错过了哪一步?我必须在另一个目录中构建它吗?

jdg4fx2g

jdg4fx2g1#

由于您在自定义位置构建了HDF5,因此您需要告诉CMake在哪里可以找到它。下面是如何修改CMakeLists.txt以包含HDF5安装路径:

cmake_minimum_required(VERSION 3.27)
project(HelloWorld)

# Set the path to your HDF5 installation
set(HDF5_ROOT "/path/to/your/hdf5/installation" CACHE PATH "Path to HDF5 installation directory")

# Find HDF5
find_package(HDF5 REQUIRED COMPONENTS CXX)

# Add the HDF5 include directory to the include path
include_directories(${HDF5_INCLUDE_DIRS})

# Link the HDF5 libraries to your executable
target_link_libraries(hello_world ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})

字符串
/path/to/your/hdf5/installation替换为HDF5安装目录的实际路径。这应该允许CMake找到HDF5库并包含目录。

相关问题