由于最近急需的一个项目,需要hdf5库,误打误撞,编译成功。特此记录
1、下载源代码
官网下载地址:https://portal.hdfgroup.org/display/support/HDF5+1.12.2#files
找到如下地址,本人电脑有cmake软件,我下载了Cmake版本和源代码版本同时进行了,事实上我也下载了源码进行cmake手工编译,但是不如直接下载CMake版本的来的方便,所以这里我直接记录了Cmake版本的编译方法
如果下载不了,可以通过该链接进行下载
2、编译步骤:
如果不想看怎么编译,可以从这里下载我编译好的库
1、下载解压(注意路径,最好不要有中文路径,个人习惯)
2、进入文件夹后,如下图
3、该文件夹下有很多批处理文件,由于我电脑上有VS2017,所以我默认使用“build-VS2017-64_debug.bat”文件。
4、点击“build-VS2017-64.bat”以管理员方式运行,剩下就是看着cmd窗口不断的编译、等待…
5、无尽等待完后,就是查看成果了。该编译的成果就在“build-VS2017-64.bat”同级文件夹的“HDF5-1.12.2-win64.zip”压缩包(我是后面改名:HDF5-1.12.2-win64_release.zip,由于这个bat处理里面是只编译Release版本,Debug版本需要再单独编译,我是在后面配置测试才发现这个问题,这里我就不记录怎么走的弯路了)
6、Debug版本编译:更改“HDF5-1.12.2-win64.zip”为“HDF5-1.12.2-win64_release.zip”,避免编译debug版本的时候被覆盖了。复制一份“build-VS2017-64.bat”改名为“build-VS2017-64_debug.bat”,用notepad打开后,将“Release”更换为“Debug”即可
7、点击“build-VS2017-64_debug.bat”以管理员方式运行,剩下就是看着cmd窗口不断的编译、等待…
8、更改“HDF5-1.12.2-win64.zip”为“HDF5-1.12.2-win64_debug.zip”,将debug压缩包和release压缩包解压,然后进行“bin”文件夹、“lib”文件夹合并即可。
Debug lib文件加载下面即可:
libaec_D.lib
libhdf5_D.lib
libhdf5_cpp_D.lib
libhdf5_hl_D.lib
libhdf5_hl_cpp_D.lib
libhdf5_tools_D.lib
libszaec_D.lib
libz_D.lib
3、测试代码
Create H5文件测试代码:
该代码
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** Copyright © 2020 Wei Wang. ** Created by WW on 2020/01/26. ** All rights reserved. ** ** This example illustrates how to create a dataset that is a 4 x 6 array. ** Reference: HDF5 Tutorial (https://portal.hdfgroup.org/display/HDF5/HDF5) ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *///// h5cpp_creating.cpp// CPP//#include <iostream>
#include <string>
#include "H5Cpp.h"#ifndef _H5_NO_NAMESPACE_
using namespace H5;
#ifndef _H5_NO_STD_
using std::cout;
using std::endl;
#endif /* _H5_NO_STD_ */
#endif /* _H5_NO_NAMESPACE_ */#define PI 3.1415926535/** Define the names of HDF5 file, groups, datasets, and attributes.* Use H5::H5std_string for name strings.*/
const H5std_string FILE_NAME("h5cpp_example.hdf5");
const H5std_string GROUP_NAME("group1");
const H5std_string DATASET_NAME("dset");
const H5std_string ATTR_NAME1("myAttr1");
const H5std_string ATTR_NAME2("myAttr2");const int DIM0 = 4; // dataset dimensions
const int DIM1 = 6;
const int RANK = 2;int main(int argc, char **argv)
{// 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();/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */double data[DIM0][DIM1]; // buffer for data to writefor (int i = 0; i < DIM0; i++)for (int j = 0; j < DIM1; j++)data[i][j] = (i + 1) * PI + j;// Create a new file using the default property lists.// H5::H5F_ACC_TRUNC : create a new file or overwrite an existing file.H5File file(FILE_NAME, H5F_ACC_TRUNC);// Create a group under root '/'.Group group(file.createGroup(GROUP_NAME));// Use H5::hsize_t (similar to int) for dimensions.hsize_t dims[RANK]; // dataset dimensionsdims[0] = DIM0;dims[1] = DIM1;// Create the dataspace for a dataset first.DataSpace dataspace(RANK, dims);// Create the dataset under group with specified dataspace. DataSet dataset = group.createDataSet(DATASET_NAME, PredType::NATIVE_DOUBLE, dataspace);// Write data in buffer to dataset.dataset.write(data, PredType::NATIVE_DOUBLE);/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */int attr1_data[2] = { 100, 200 }; // buffer for attribute data to wirtehsize_t attr1_dims[1] = { 2 }; // attribute dimension, rank = 1// Create the dataspace for an attribute first.DataSpace attr1_dataspace(1, attr1_dims); // rank = 1// Create the attribute of dataset with specified dataspace.Attribute attribute1 = dataset.createAttribute(ATTR_NAME1, PredType::STD_I32BE, attr1_dataspace);// Write data in buffer to attribute.attribute1.write(PredType::NATIVE_INT, attr1_data);/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//* String Data */char attr2_data[30]; // buffer for attribute data to wirte sprintf(attr2_data, "hello, world!\nAuthor: Wei Wang");hsize_t attr2_dims[1] = { 30 }; // attribute dimension, rank = 1// Create the dataspace for an attribute first.DataSpace attr2_dataspace(1, attr2_dims); // rank = 1// Create the attribute of dataset with specified dataspace.Attribute attribute2 = dataset.createAttribute(ATTR_NAME2, PredType::NATIVE_CHAR, attr2_dataspace);// Write data in buffer to attribute.attribute2.write(PredType::NATIVE_CHAR, attr2_data);/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */// Save and exit the group.group.close();// Save and exit the file.file.close();/* h5cpp_example.hdf5 file structure* +-- '/'* | +-- group 'group1'* | | +-- dataset 'dset'* | | | +-- attribute 'myAttr1'* | | | +-- attribute 'myAttr2'* | | |* | |* |*/} // 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;}return 0; // successfully terminated}
H5 Reading测试代码,里面一段代码需要改一下才可以用,我用Create h5代码测试可以了就没有继续改了
double data_out[dims[0]][dims[1]];
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** Copyright © 2020 Wei Wang. ** Created by WW on 2020/01/26. ** All rights reserved. ** ** This example illustrates how to read and edit an existing dataset. ** Reference: HDF5 Tutorial (https://portal.hdfgroup.org/display/HDF5/HDF5) ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *///// h5cpp_reading.cpp// CPP//#include <iostream>
#include <string>
#include "H5Cpp.h"#ifndef _H5_NO_NAMESPACE_
using namespace H5;
#ifndef _H5_NO_STD_
using std::cout;
using std::endl;
#endif /* _H5_NO_STD_ */
#endif /* _H5_NO_NAMESPACE_ *//** Define the names of HDF5 file, groups, datasets, and attributes.* Use H5::H5std_string for name strings.*/
const H5std_string FILE_NAME("h5cpp_example.hdf5");
const H5std_string GROUP_NAME("group1");
const H5std_string DATASET_NAME("dset");
const H5std_string ATTR_NAME("myAttr2");int main(int argc, char **argv)
{// 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();/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//* HOW TO DELETING A DATASET! *//*// Open an existing file.// H5::H5F_ACC_RDWR : read or edit an existing file.H5File file_d(FILE_NAME, H5F_ACC_RDWR);// Open an existing group.Group group_d = file_d.openGroup(GROUP_NAME);// Use H5::H5Ldelete to delete an existing dataset.int result = H5Ldelete(group_d.getId(), DATASET_NAME.c_str(), H5P_DEFAULT);// String.c_str() convert "string" to "const char *".cout << result << endl;// Non-negtive: successfully delete;// Otherwise: fail.// Save and exit the group.group_d.close();// Save and exit the file.file_d.close();// Important! The two close()s above can't be omitted!// Otherwise, the deleting behavior won't be saved to file.*//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */// Open an existing file.// H5::H5F_ACC_RDWR : read or edit an existing file.H5File file(FILE_NAME, H5F_ACC_RDWR);// Open an existing group of the file.Group group = file.openGroup(GROUP_NAME);// Open an existing dataset of the group.DataSet dataset = group.openDataSet(DATASET_NAME);// Get the dataspace of the dataset.DataSpace filespace = dataset.getSpace();// Get the rank of the dataset.int rank = filespace.getSimpleExtentNdims();// Use H5::hsize_t (similar to int) for dimensions//hsize_t dims[rank]; // dataset dimensionshsize_t* dims=new hsize_t[rank]; // dataset dimensions// Get the dimensions of the dataset.rank = filespace.getSimpleExtentDims(dims);cout << DATASET_NAME << " rank = " << rank << ", dimensions "<< dims[0] << " x "<< dims[1] << endl;// Dataspace for data read from file.DataSpace myspace(rank, dims);//下面这个代码定义有问题,不能使用变量当作常数这么定义,需要改一下double data_out[dims[0]][dims[1]]; // buffer for data read from file//=======================================================================================================//double** data_out=new double*[dims[0]]; // 这一段代码改写可能存在问题//for (int id = 0; id < dims[0]; id++)//{// data_out[id] = new double[dims[1]];//}//=======================================================================================================// Read data from file to buffer.dataset.read(data_out, PredType::NATIVE_DOUBLE, myspace, filespace);for (int i = 0; i < dims[0]; i++){for (int j = 0; j < dims[1]; j++)cout << data_out[i][j] << " ";cout << endl;}/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */// Read the attribute of the dataset.cout << endl;// Open an existing attribute of the dataset.Attribute attr = dataset.openAttribute(ATTR_NAME);// Get the dataspace of the attribute.DataSpace attr_space = attr.getSpace();// Get the rank of the attribute.int attr_rank = attr_space.getSimpleExtentNdims();// Use H5::hsize_t (similar to int) for dimensions.//hsize_t attr_dims[attr_rank]; // attribute dimensionshsize_t* attr_dims=new hsize_t[attr_rank]; //改写上面一句代码// Get the dimension of the attribute.attr_rank = attr_space.getSimpleExtentDims(attr_dims);cout << ATTR_NAME << " rank = " << attr_rank << ", dimensions " << attr_dims[0] << endl;//char attr_data_out[attr_dims[0]]; // buffer for attribute data read from filechar* attr_data_out=new char[attr_dims[0]]; // buffer for attribute data read from file// Read attribute data from file to buffer. attr.read(PredType::NATIVE_CHAR, attr_data_out);cout << attr_data_out << endl;/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */// Save and exit the group.group.close();// Save and exit the file.file.close();} // 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;}return 0; // successfully terminated}