-1

I should preface that I am not so familiar with compiling programs in C++ that include libraries outside of the standard library. For the past couple of years I have worked almost exclusively with python.

In my experience, h5py is a bit slow because python is a bit slow in I/O. So I want to learn how to rewrite all of my hdf5 python code in c++ to speed things up.

My first attempt at it was just to compile the code found here https://support.hdfgroup.org/HDF5/doc/cpplus_RM/create_8cpp-example.html.

My compilation command is

$> g++ -L /path/to/lib -lhdf5_cpp -lhdf5 -I /path/to/include create.cpp -o create

However I just get a wall of text with the error message, such as

undefined reference to `H5::Exception::dontPrint()
...
/usr/bin/ld: create.cpp:(.text+0x28b): undefined reference to `H5::H5File::~H5File()

If you guys want I can send a wall of text to give more information. In case this is enough information though, do you guys have any recommendation as to how I should go about compiling the program?

user3166083
  • 147
  • 8
  • [Your linkage consumes libraries before the object files that refer to them](https://stackoverflow.com/a/43305704/1362568) – Mike Kinghan May 12 '20 at 18:42

1 Answers1

2

Firstly, I would try to compile with its own compiler. I slightly changed your code.

--> create.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
}

In my case it was here:

/usr/local/opt/hdf5@1.10/bin/h5c++  create.cpp -o create

After execution ./create, it produced file SDS.h5.

The file contains:

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)]

After that, you can try to figure out what goes wrong. First place, in my opinion, should be your configuration of the HDF5.

h5c++ -showconfig
Alexander
  • 21
  • 3