我很难获得使用 Visual Studio 2013 (C++) 的 HDF5 示例。
示例位于:http://www.hdfgroup.org/ftp/HDF5/examples/misc-examples/stratt.cpp为了完整起见,我已经发布了下面的代码。
我的第一个问题是:最新的 HDF5(版本 1.8.13)是否适用于 Visual C++ 2013?文档只提到了我能看到的 2012 年,但通常我在使用 2013 年时没有遇到任何问题,其中提到了 2012 年。
我尝试将示例程序用作 32 位和 64 位应用程序。最终,我对 64 位感兴趣。在32位的项目设置中,在VC++设置下,我在include目录中添加了:C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\include
在库目录中,我添加了:C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\lib
到链接器->输入,我添加了:hdf5.lib;hdf5_cpp.lib
当我运行时,我(不出所料)收到消息,“程序无法启动,因为您的计算机缺少 hdf5.dll...”
所以到调试目录,我从目录中添加了 hdf5.dll 和 hdf5_cpp.dll:
C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\bin
然后我得到运行时错误:
应用程序无法正确启动 (0xc000007b)。单击确定关闭应用程序。有任何想法吗?
顺便说一句,当我尝试 x64 位版本(使用 64 位设置、目录和文件)时,我得到了稍微不同的错误。程序运行到最后,但没有属性写入控制台,没有生成文件,最后我得到了可怕的错误(在最后一行点击 f10 后):
HDF5AttributeExample2.exe 中 0x000007FEF05E512D (msvcp120d.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF。
在这两种情况下(32 位和 64 位)我的直觉告诉我我有某种配置问题(错误的库,错误的设置等)我真的很感激人们可以提供的任何帮助或建议。
如果还有另一个 Visual C++ HDF5 示例,请务必告诉我!
谢谢,
戴夫
#include <string>
#include <iostream>
#include "H5Cpp.h"
using std::cout;
using std::endl;
using namespace H5;
const H5std_string FILE_NAME("string_test.h5");
const H5std_string DS_NAME("Data Set 1");
const H5std_string ATTR_NAME("String Attribute");
int main(void) {
// Create the named file
H5File file = H5File(FILE_NAME, H5F_ACC_TRUNC);
// Create new dataspace for the dataset
const int rank = 3;
const int dim1 = 2;
const int dim2 = 2;
const int dim3 = 2;
hsize_t dims[rank] = { dim1, dim2, dim3 };
DataSpace dataspace = DataSpace(rank, dims);
// Create new datatype for the dataset
FloatType datatype(PredType::NATIVE_FLOAT);
// Create the dataset
DataSet dataset = file.createDataSet(DS_NAME, datatype, dataspace);
// Set up write buffer 'matrix'
int q, r, s;
float value;
float matrix[dim1][dim2][dim3];
for (q = 0; q < dim1; q++)
for (r = 0; r < dim2; r++)
for (s = 0; s < dim3; s++)
{
value = 1.111 + (q * r * s);
matrix[q][r][s] = value;
}
// Write data to the dataset
dataset.write(matrix, datatype);
// Create new dataspace for attribute
DataSpace attr_dataspace = DataSpace(H5S_SCALAR);
// Create new string datatype for attribute
StrType strdatatype(PredType::C_S1, 256); // of length 256 characters
// Set up write buffer for attribute
const H5std_string strwritebuf("This attribute is of type StrType");
// Create attribute and write to it
Attribute myatt_in = dataset.createAttribute(ATTR_NAME, strdatatype, attr_dataspace);
myatt_in.write(strdatatype, strwritebuf);
// Set up read buffer for attribute
H5std_string strreadbuf("");
// Open attribute and read its contents
Attribute myatt_out = dataset.openAttribute(ATTR_NAME);
myatt_out.read(strdatatype, strreadbuf);
// Display attribute contents
cout << "Attribute contents: " << strreadbuf << endl;
return 0;
}
最佳答案
您遇到的运行时错误是因为您使用的是用 VS2012 编译的库。
来自 HDF 集团 website在 VisualStudio 和 CMake 上:
First, make sure you use the same version of Visual Studio that was used to create the pre-built binaries. This is required to avoid runtime errors.
我建议你自己尝试使用 CMake 在 VS2013 上构建源代码。
好的,在尝试为 VS2013 Ultimate 编译它之后,我设法让它同时支持 32 位和 64 位。
"
cpack -C {Debug | Release} ---config C:\...\"build"\CPackConfig.cmake
。这将为文件创建一个安装程序。C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\include
.相应地更改此路径。如果您使用的是 x64 版本,则需要将此窗口顶部的平台更改为 x64 .如果它不存在,请单击配置管理器,然后从事件解决方案平台下拉菜单中选择新建。添加 x64 选项。 C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\lib
.也相应地改变这一点。请注意,如果您为 x64 构建二进制文件,它们将安装在 C:\Program Files\HDF_Group\HDF5\1.8.13\
. 祝你好运!
关于visual-c++ - 使用 Visual C++ 2013 在 Visual Studio 2013 上开始使用 HDF5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24325508/