getting stared with HDF5 on Visual Studio 2013 with Visual C++ 2013

4

I am having a hard time getting an HDF5 example working with Visual Studio 2013 (C++). The example is at: http://www.hdfgroup.org/ftp/HDF5/examples/misc-examples/stratt.cpp and I've posted the code below for completeness.

My first question is: Will the latest HDF5 (version 1.8.13) work with Visual C++ 2013? The docs only mention 2012 that I can see, but generally I've had no problems using 2013 where 2012 is mentioned.

I tried the example program as both a 32 bit and a 64 bit app. Ultimately, I'm interested in 64 bit. In the project settings for 32 bit, under VC++ settings, I added to the include directories: C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\include To the library directories, I added: C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\lib

To the Linker->Input, I added: hdf5.lib;hdf5_cpp.lib When I ran, I (not unexpectedly) got the message, "The program can't start because hdf5.dll is missing from your computer..." So to the debug directory, I added, hdf5.dll and hdf5_cpp.dll from the directory: C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\bin

I then get the runtime error:

The application was unable to start correctly (0xc000007b). Click OK to close the application. Any ideas?

Incidentally, when I tried the x64 bit version (using the 64 bit setttings, directories and files), I got slightly different errors. The program runs to the end, but no attribute is written to the console, no file is produced, and I get the dreaded error at the end (after hitting f10 on the last line):

Unhandled exception at 0x000007FEF05E512D (msvcp120d.dll) in HDF5AttributeExample2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

In both cases (32 and 64 bit) my gut tells me that I have some sort of configuration problem (wrong lib, wrong setup, etc.) I'd really appreciate any help or suggestions folks can offer.

If there is another Visual C++ HDF5 example, by all means please tell me!

Thanks,

Dave

#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;
}
visual-c++
visual-studio-2013
hdf5
asked on Stack Overflow Jun 20, 2014 by Dave • edited Jun 20, 2014 by Dave

2 Answers

4

The runtime error that you are getting arises from the fact that you are using libraries that were compiled with VS2012.

From the HDF Group website on VisualStudio and 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.

I suggest that you try by yourself to build the source code on VS2013 using CMake.


Ok, after having tried myself to compile it for the VS2013 Ultimate I managed to get it working with both 32 bit and 64 bit support.

  • Download the source code and unzip it. Let's assume the directory name is "source".
  • Download CMake and open the GUI, choose the source location and choose a "build" location.
  • Click Configure and select the version from visual studio you are using.
  • The configuration options will appear in red on CMake. Choose whatever you need.
  • Click on generate.
  • Go to the "build" folder and open the generated MSVS solution. Build the solution.
  • In the same build folder run the command "cpack -C {Debug | Release} ---config C:\...\"build"\CPackConfig.cmake. This will create an installer for the files.
  • Run this installer.
  • Create an empty project in MSVS. Go to Project Configuration Properties. Then select C/C++ -> General -> Additional Include Directories and insert C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\include. Change this path accordingly. If you are using a x64 version, you will need to change the platform on top of this window to x64. If it doesn't exist, click on Configuration Manager and from the Active Solution platform drop down menu choose New. Add the x64 option.
  • Now go to Linker - > General -> Additional Library Directories: Add C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\lib. Change this also accordingly. Note that if you built the binaries for x64 they will be installed at C:\Program Files\HDF_Group\HDF5\1.8.13\.
  • Got to Linker -> Input -> Additional Dependencies. Add libhdf5_D.lib and libhdf5_cpp_D.lib if you built it as debug, otherwise (I believe) use hdf5.lib and hdf5_cpp.lib.

Good Luck!

answered on Stack Overflow Aug 4, 2014 by Rick • edited Nov 13, 2017 by Carsten
3

Doesn't look like too many people are interested in HDF5 with Visual C++ judging by the number of views. Perhaps that should be a warning to me! In any case, for posterity I offer the following answers to my original question.

First of all, one needs to be very careful that the path variable is correct in environments settings AND that it is set up correctly for 32 bit or 64 bit depending on which you want.
Instead of: C:\Program Files (x86)\HDF_Group\HDF5\1.8.13\bin I have: C:\Program Files (x86)\HDF Group\HDF5\1.8.13\bin (notice missing underscore)

Additionally, one has to pay attention as to where you want 32 bit (use Program File (x86)) or 64 bit (use Program Files). If anyone knows of an easier way to run 32 bit and 64 bit programs on the same machine that use HDf5, please let me know.

The other major problem was with the example code itself. In it's original form, it did not work. After much trial and error, I tracked the problem down to the use of H5std_string. When I replaced these with char [], everything worked. As an example, instead of using: const H5std_string FILE_NAME("string_test.h5"); I used: char fileName[128] = "string_test.h5"; and used H5File constructor as so: H5File file = H5File(fileName, H5F_ACC_TRUNC); It's annoying that I didn't get a compile time error. The constructor claims to take std::string (which is what H5std_string is) in the constructor for H5File. Quite possibly, there is some Visual Studio setting somewhere that makes things the original code work. I'm not knowledgeable enough about Visual C++ to know.

Anyhow good luck all.

Dave

answered on Stack Overflow Jun 20, 2014 by Dave

User contributions licensed under CC BY-SA 3.0