安装 HDF5 for C++。

huangapple go评论65阅读模式
英文:

installing HDF5 for C++

问题

我正在尝试使用HDF5编写C++程序,但在构建过程中遇到了困难。似乎cmake找不到HDF5库。以下是我尝试过的内容:

main.cpp:

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

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

CMakeLists.txt:

project(HelloWorld)
cmake_minimum_required(VERSION 3.27)

add_executable(hello_world main.cpp)

# 在这里我需要做什么?find_package(HDF5 REQUIRED) 失败了,因为它找不到HDF5库
include_directories(${HDF5_INCLUDE_DIRS})
find_package(HDF5 REQUIRED)
target_link_libraries(hello_world ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})

运行cmake会失败,并显示以下错误消息:

无法找到HDF5(缺少:HDF5_LIBRARIES HDF5_INCLUDE_DIRS)(找到的版本“”)

我在.local/hdf5文件夹中下载并构建了HDF5(我使用的是Ubuntu),步骤如下:

mkdir build
cd build
cmake ..
make

我漏掉了哪个步骤?我需要在另一个目录中构建它吗?

英文:

I'm trying to write a C++ program using HDF5, but I struggle with the build. It seems like cmake doesn't find the HDF5 library. Here is what I tried:

main.cpp:

#include &lt;iostream&gt;
#include &lt;hdf5.h&gt;

int main(){
    std::cout &lt;&lt; &quot;Hello World&quot; &lt;&lt; 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})

Running cmake fails with the error message

Could NOT find HDF5 (missing: HDF5_LIBRARIES HDF5_INCLUDE_DIRS) (found version &quot;&quot;)

I downloaded and built HDF5 inside the folder .local/hdf5 (I'm using Ubuntu) using

mkdir build
cd build
cmake ..
make

What step am I missing? Do I have to build it in another directory?

答案1

得分: 1

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

cmake_minimum_required(VERSION 3.27)
project(HelloWorld)

# 设置HDF5安装路径
set(HDF5_ROOT "/path/to/your/hdf5/installation" CACHE PATH "HDF5安装目录的路径")

# 查找HDF5
find_package(HDF5 REQUIRED COMPONENTS CXX)

# 将HDF5包含目录添加到包含路径中
include_directories(${HDF5_INCLUDE_DIRS})

# 将HDF5库链接到您的可执行文件
target_link_libraries(hello_world ${HDF5_CXX_LIBRARIES} ${HDF5_LIBRARIES})

请将/path/to/your/hdf5/installation替换为您的HDF5安装目录的实际路径。这应该使CMake能够找到HDF5库和包含目录。

英文:

Since you built HDF5 in a custom location, you need to tell CMake where to find it. Here's how you can modify your CMakeLists.txt to include the path to your HDF5 installation:

cmake_minimum_required(VERSION 3.27)
project(HelloWorld)

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

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

Replace /path/to/your/hdf5/installation with the actual path to your HDF5 installation directory. This should allow CMake to find the HDF5 libraries and include directories.

huangapple
  • 本文由 发表于 2023年7月23日 17:16:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定