英文:
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 <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})
Running cmake fails with the error message
Could NOT find HDF5 (missing: HDF5_LIBRARIES HDF5_INCLUDE_DIRS) (found version "")
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 "/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})
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论