c++ - 使用 g++ 编译 hdf5 源代码
问题描述
我应该先说我不太熟悉用 C++ 编译包含标准库之外的库的程序。在过去的几年里,我几乎只使用 python。
根据我的经验,h5py 有点慢,因为 python 在 I/O 方面有点慢。所以我想学习如何用 c++ 重写我所有的 hdf5 python 代码以加快速度。
我的第一次尝试只是编译在这里找到的代码https://support.hdfgroup.org/HDF5/doc/cpplus_RM/create_8cpp-example.html。
我的编译命令是
$> g++ -L /path/to/lib -lhdf5_cpp -lhdf5 -I /path/to/include create.cpp -o create
但是我只是收到一堵带有错误消息的文字墙,例如
undefined reference to `H5::Exception::dontPrint()
...
/usr/bin/ld: create.cpp:(.text+0x28b): undefined reference to `H5::H5File::~H5File()
如果你们愿意,我可以发送一堵文字墙以提供更多信息。如果这是足够的信息,你们对我应该如何编译程序有什么建议吗?
解决方案
首先,我会尝试使用它自己的编译器进行编译。我稍微改变了你的代码。
--> 创建.cpp
#include <iostream>
#include <string>
#include "H5Cpp.h"
using namespace H5;
const H5std_string FILE_NAME( "SDS.h5" );
const H5std_string DATASET_NAME( "IntArray" );
const int NX = 5; // dataset dimensions
const int NY = 6;
const int RANK = 2;
int main (void)
{
/*
* Data initialization.
*/
int i, j;
int data[NX][NY]; // buffer for data to write
for (j = 0; j < NX; j++)
{
for (i = 0; i < NY; i++)
data[j][i] = i + j;
}
/*
* 0 1 2 3 4 5
* 1 2 3 4 5 6
* 2 3 4 5 6 7
* 3 4 5 6 7 8
* 4 5 6 7 8 9
*/
// Try block to detect exceptions raised by any of the calls inside it
try
{
/*
* Turn off the auto-printing when failure occurs so that we can
* handle the errors appropriately
*/
Exception::dontPrint();
/*
* Create a new file using H5F_ACC_TRUNC access,
* default file creation properties, and default file
* access properties.
*/
H5File file( FILE_NAME, H5F_ACC_TRUNC );
/*
* Define the size of the array and create the data space for fixed
* size dataset.
*/
hsize_t dims[2]; // dataset dimensions
dims[0] = NX;
dims[1] = NY;
DataSpace dataspace( RANK, dims );
/*
* Define datatype for the data in the file.
* We will store little endian INT numbers.
*/
IntType datatype( PredType::NATIVE_INT );
datatype.setOrder( H5T_ORDER_LE );
/*
* Create a new dataset within the file using defined dataspace and
* datatype and default dataset creation properties.
*/
DataSet dataset = file.createDataSet( DATASET_NAME, datatype, dataspace );
/*
* Write the data to the dataset using default memory space, file
* space, and transfer properties.
*/
dataset.write( data, PredType::NATIVE_INT );
} // end of try block
// catch failure caused by the H5File operations
catch( FileIException error )
{
error.printErrorStack();
return -1;
}
// catch failure caused by the DataSet operations
catch( DataSetIException error )
{
error.printErrorStack();
return -1;
}
// catch failure caused by the DataSpace operations
catch( DataSpaceIException error )
{
error.printErrorStack();
return -1;
}
// catch failure caused by the DataSpace operations
catch( DataTypeIException error )
{
error.printErrorStack();
return -1;
}
return 0; // successfully terminated
}
就我而言,它在这里:
/usr/local/opt/hdf5@1.10/bin/h5c++ create.cpp -o create
执行后./create
,生成文件SDS.h5
。
该文件包含:
In [1]: import h5py; f = h5py.File("SDS.h5")
In [2]: list(f["IntArray"])
Out[2]:
[array([0, 1, 2, 3, 4, 5], dtype=int32),
array([1, 2, 3, 4, 5, 6], dtype=int32),
array([2, 3, 4, 5, 6, 7], dtype=int32),
array([3, 4, 5, 6, 7, 8], dtype=int32),
array([4, 5, 6, 7, 8, 9], dtype=int32)]
之后,您可以尝试找出问题所在。首先,在我看来,应该是您对 HDF5 的配置。
h5c++ -showconfig
推荐阅读
- reactjs - NextJs CORS 问题
- git - Git文件夹和github存储库连接问题
- tensorflow - 使用 GradientTape TensorFlow 计算二阶导数
- typescript - 为什么“任何”类型扩展另一种类型将始终是打字稿中的联合类型?
- c# - 继承类型与Generics类型之间的类型转换
- excel - 如何在数组中复制存储的“特定单元格”?每个(循环)范围的替代方案
- mysql - 如何获取包含最大值且不重复且具有分组依据的数据?
- php - Laravel 8我们如何获得经过身份验证的用户电子邮件地址
- docker - 查找 docker 登录用户的速率限制
- flutter - 列表小部件状态未在颤振中使用 StatefulBuilder 更新