diff -r 87c3848cf3c0 configure.ac
--- a/configure.ac	Tue Jun 10 14:03:09 2014 -0700
+++ b/configure.ac	Thu Jun 12 18:00:26 2014 +0200
@@ -893,11 +893,11 @@
 
 save_CPPFLAGS="$CPPFLAGS"
 save_LIBS="$LIBS"
-CPPFLAGS="$Z_CPPFLAGS $CPPFLAGS"
-LIBS="$Z_LDFLAGS $Z_LIBS $LIBS"
+CPPFLAGS="$HDF5_CPPFLAGS $Z_CPPFLAGS $CPPFLAGS"
+LIBS="$HDF5_LDFLAGS $Z_LDFLAGS $HDF5_LIBS $Z_LIBS $LIBS"
 OCTAVE_CHECK_LIB(hdf5, HDF5,
   [HDF5 library not found.  Octave will not be able to save or load HDF5 data files.],
-  [hdf5.h], [H5Gget_num_objs], [], [],
+  [hdf5.h], [H5open], [], [],
   [warn_hdf5=
    OCTAVE_CHECK_HDF5_HAS_VER_16_API
    TEXINFO_HDF5="@set HAVE_HDF5"
diff -r 87c3848cf3c0 libinterp/corefcn/load-save.cc
--- a/libinterp/corefcn/load-save.cc	Tue Jun 10 14:03:09 2014 -0700
+++ b/libinterp/corefcn/load-save.cc	Thu Jun 12 18:00:26 2014 +0200
@@ -1265,7 +1265,11 @@
             if (format == LS_HDF5)
               {
                 hdf5_ofstream& hs = dynamic_cast<hdf5_ofstream&> (os);
+#ifdef HAVE_HDF5_18
+                H5Oset_comment (hs.file_id, comment_string.c_str ());
+#else
                 H5Gset_comment (hs.file_id, "/", comment_string.c_str ());
+#endif
               }
             else
 #endif /* HAVE_HDF5 */
diff -r 87c3848cf3c0 libinterp/corefcn/ls-hdf5.cc
--- a/libinterp/corefcn/ls-hdf5.cc	Tue Jun 10 14:03:09 2014 -0700
+++ b/libinterp/corefcn/ls-hdf5.cc	Thu Jun 12 18:00:26 2014 +0200
@@ -145,13 +145,13 @@
 #if HAVE_HDF5_18
   H5Eget_auto (H5E_DEFAULT, &err_func, &err_func_data);
   H5Eset_auto (H5E_DEFAULT, 0, 0);
+  hid_t attr_id = H5Aopen (loc_id, attr_name, H5P_DEFAULT);
 #else
   H5Eget_auto (&err_func, &err_func_data);
   H5Eset_auto (0, 0);
+  hid_t attr_id = H5Aopen_name (loc_id, attr_name);
 #endif
 
-  hid_t attr_id = H5Aopen_name (loc_id, attr_name);
-
   if (attr_id >= 0)
     {
       // successful
@@ -187,13 +187,13 @@
 #if HAVE_HDF5_18
   H5Eget_auto (H5E_DEFAULT, &err_func, &err_func_data);
   H5Eset_auto (H5E_DEFAULT, 0, 0);
+  hid_t attr_id = H5Aopen (loc_id, attr_name,H5P_DEFAULT);
 #else
   H5Eget_auto (&err_func, &err_func_data);
   H5Eset_auto (0, 0);
+  hid_t attr_id = H5Aopen_name (loc_id, attr_name);
 #endif
 
-  hid_t attr_id = H5Aopen_name (loc_id, attr_name);
-
   if (attr_id >= 0)
     {
       hid_t space_id = H5Aget_space (attr_id);
@@ -234,18 +234,24 @@
   return type_id;
 }
 
-// This function is designed to be passed to H5Giterate, which calls it
+// This function is designed to be passed to H5Giterate (HDF5 API v16)
+// or H5Literate (HDF API v18), which calls it
 // on each data item in an HDF5 file.  For the item whose name is NAME in
 // the group GROUP_ID, this function sets dv->tc to an Octave representation
 // of that item.  (dv must be a pointer to hdf5_callback_data.)  (It also
 // sets the other fields of dv).
 //
 // It returns 1 on success (in which case H5Giterate stops and returns),
-// -1 on error, and 0 to tell H5Giterate to continue on to the next item
-// (e.g. if NAME was a data type we don't recognize).
+// -1 on error, and 0 to tell H5Giterate/H5Literate to continue on to the
+// next item (e.g. if NAME was a data type we don't recognize).
 
+#if HAVE_HDF5_18
+herr_t
+hdf5_read_next_data (hid_t group_id, const char *name, const H5L_info_t*, void *dv)
+#else
 herr_t
 hdf5_read_next_data (hid_t group_id, const char *name, void *dv)
+#endif
 {
   hdf5_callback_data *d = static_cast <hdf5_callback_data *> (dv);
   hid_t type_id = -1;
@@ -254,7 +260,6 @@
   hid_t subgroup_id = -1;
   hid_t space_id = -1;;
 
-  H5G_stat_t info;
   herr_t retval = 0;
   bool ident_valid = valid_identifier (name);
 
@@ -272,9 +277,19 @@
       ident_valid = valid_identifier (vname);
     }
 
+#if HAVE_HDF5_18
+  H5O_info_t info;
+  H5Oget_info_by_name (group_id, name, &info, H5P_DEFAULT);
+  static H5O_type_t type_group = H5O_TYPE_GROUP;
+  static H5O_type_t type_dataset = H5O_TYPE_DATASET;
+#else
+  H5G_stat_t info;
   H5Gget_objinfo (group_id, name, 1, &info);
+  static H5G_obj_t type_group = H5G_GROUP;
+  static H5G_obj_t type_dataset = H5G_DATASET;
+#endif
 
-  if (info.type == H5G_GROUP && ident_valid)
+  if (info.type == type_group && ident_valid)
     {
 #if HAVE_HDF5_18
       subgroup_id = H5Gopen (group_id, name, H5P_DEFAULT);
@@ -361,7 +376,7 @@
         }
 
     }
-  else if (info.type == H5G_DATASET && ident_valid)
+  else if (info.type == type_dataset && ident_valid)
     {
       // For backwards compatiability.
 #if HAVE_HDF5_18
@@ -555,12 +570,22 @@
   if (retval > 0)
     {
       // get documentation string, if any:
+      // note that H5Oget_comment_by_name returns the length WITHOUT terminator,
+      // but H5Gget_comment returns the length WITH terminator ...
+#ifdef HAVE_HDF5_18
+      ssize_t comment_length = H5Oget_comment_by_name (group_id, name, NULL, 0, H5P_DEFAULT) + 1;
+#else
       int comment_length = H5Gget_comment (group_id, name, 0, 0);
+#endif
 
       if (comment_length > 1)
         {
           OCTAVE_LOCAL_BUFFER (char, tdoc, comment_length);
+#ifdef HAVE_HDF5_18
+          H5Oget_comment_by_name (group_id, name, tdoc, comment_length, H5P_DEFAULT);
+#else
           H5Gget_comment (group_id, name, comment_length, tdoc);
+#endif
           d->doc = tdoc;
         }
       else if (vname != name)
@@ -594,16 +619,26 @@
   hdf5_ifstream& hs = dynamic_cast<hdf5_ifstream&> (is);
   hdf5_callback_data d;
 
-  herr_t H5Giterate_retval = -1;
+  herr_t H5Xiterate_retval = -1;
+
+#if HAVE_HDF5_18
+  hid_t group_id = H5Gopen (hs.file_id, "/", H5P_DEFAULT);
+  
+  H5G_info_t group_info;
+  
+  if ( H5Gget_info (group_id, &group_info) < 0 )
+    return NULL;
+
+  if (hs.current_item < group_info.nlinks)
+    H5Xiterate_retval = H5Literate (group_id, H5_INDEX_NAME, 
+                                    H5_ITER_INC, &hs.current_item, 
+                                    hdf5_read_next_data, &d);
+#else
+  hid_t group_id = H5Gopen (hs.file_id, "/");
 
   hsize_t num_obj = 0;
-#if HAVE_HDF5_18
-  hid_t group_id = H5Gopen (hs.file_id, "/", H5P_DEFAULT);
-#else
-  hid_t group_id = H5Gopen (hs.file_id, "/");
-#endif
+
   H5Gget_num_objs (group_id, &num_obj);
-  H5Gclose (group_id);
 
   // For large datasets and out-of-core functionality,
   // check if only parts of the data is requested
@@ -636,10 +671,12 @@
 
 
   if (hs.current_item < static_cast<int> (num_obj))
-    H5Giterate_retval = H5Giterate (hs.file_id, "/", &hs.current_item,
+    H5Xiterate_retval = H5Giterate (hs.file_id, "/", &hs.current_item,
                                     hdf5_read_next_data, &d);
+#endif
 
-  if (H5Giterate_retval > 0)
+  H5Gclose (group_id);
+  if (H5Xiterate_retval > 0)
     {
       global = d.global;
       tc = d.tc;
@@ -647,7 +684,7 @@
     }
   else
     {
-      // an error occurred (H5Giterate_retval < 0) or there are no
+      // an error occurred (H5Xiterate_retval < 0) or there are no
       // more datasets print an error message if retval < 0?
       // hdf5_read_next_data already printed one, probably.
     }
@@ -912,9 +949,15 @@
   retval = val.save_hdf5 (data_id, "value", save_as_floats);
 
   // attach doc string as comment:
+#ifdef HAVE_HDF5_18
+  if (retval && doc.length () > 0
+      && H5Oset_comment (data_id, doc.c_str ()) < 0)
+    retval = false;
+#else
   if (retval && doc.length () > 0
       && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0)
     retval = false;
+#endif
 
   // if it's global, add an attribute "OCTAVE_GLOBAL" with value 1
   if (retval && mark_as_global)
diff -r 87c3848cf3c0 libinterp/corefcn/ls-hdf5.h
--- a/libinterp/corefcn/ls-hdf5.h	Tue Jun 10 14:03:09 2014 -0700
+++ b/libinterp/corefcn/ls-hdf5.h	Thu Jun 12 18:00:26 2014 +0200
@@ -41,7 +41,11 @@
   hid_t file_id;
 
   // keep track of current item index in the file
+#if HAVE_HDF5_18
+  hsize_t current_item;
+#else
   int current_item;
+#endif
 
   hdf5_fstreambase () : file_id (-1), current_item () { }
 
@@ -164,8 +168,13 @@
 extern OCTINTERP_API bool
 hdf5_types_compatible (hid_t t1, hid_t t2);
 
+#if HAVE_HDF5_18
+extern OCTINTERP_API herr_t
+hdf5_read_next_data (hid_t group_id, const char *name, const H5L_info_t*, void *dv);
+#else
 extern OCTINTERP_API herr_t
 hdf5_read_next_data (hid_t group_id, const char *name, void *dv);
+#endif
 
 extern OCTINTERP_API bool
 add_hdf5_data (hid_t loc_id, const octave_value& tc,
diff -r 87c3848cf3c0 libinterp/octave-value/ov-cell.cc
--- a/libinterp/octave-value/ov-cell.cc	Tue Jun 10 14:03:09 2014 -0700
+++ b/libinterp/octave-value/ov-cell.cc	Thu Jun 12 18:00:26 2014 +0200
@@ -1227,25 +1227,39 @@
 
   Cell m (dv);
 
+#if HAVE_HDF5_18
+  hsize_t current_item = 0;
+  H5G_info_t group_info;
+
+  group_id = H5Gopen (loc_id, name, H5P_DEFAULT);
+
+  if ( H5Gget_info (group_id, &group_info) < 0 )
+    return false;
+#else
   int current_item = 0;
+  hsize_t num_obj = 0;
 
-  hsize_t num_obj = 0;
-#if HAVE_HDF5_18
-  group_id = H5Gopen (loc_id, name, H5P_DEFAULT);
-#else
   group_id = H5Gopen (loc_id, name);
+  H5Gget_num_objs (group_id, &num_obj);
 #endif
-  H5Gget_num_objs (group_id, &num_obj);
-  H5Gclose (group_id);
 
   for (octave_idx_type i = 0; i < dv.numel (); i++)
     {
 
+#if HAVE_HDF5_18
+      if (current_item >= group_info.nlinks)
+        retval2 = -1;
+      else
+        retval2 = H5Literate (group_id, H5_INDEX_NAME,
+                              H5_ITER_INC, &current_item,
+                              hdf5_read_next_data, &dsub);
+#else
       if (current_item >= static_cast<int> (num_obj))
         retval2 = -1;
       else
         retval2 = H5Giterate (loc_id, name, &current_item,
                               hdf5_read_next_data, &dsub);
+#endif
 
       if (retval2 <= 0)
         break;
@@ -1255,6 +1269,8 @@
 
     }
 
+  H5Gclose (group_id);
+
   if (retval2 >= 0)
     {
       matrix = m;
diff -r 87c3848cf3c0 libinterp/octave-value/ov-class.cc
--- a/libinterp/octave-value/ov-class.cc	Tue Jun 10 14:03:09 2014 -0700
+++ b/libinterp/octave-value/ov-class.cc	Thu Jun 12 18:00:26 2014 +0200
@@ -1592,10 +1592,15 @@
 
   herr_t retval2 = 0;
   octave_map m (dim_vector (1, 1));
-  int current_item = 0;
-  hsize_t num_obj = 0;
   int slen = 0;
   hsize_t rank = 0;
+#if HAVE_HDF5_18
+  hsize_t current_item = 0;
+  H5G_info_t subgroup_info;
+#else
+  hsize_t num_obj = 0;
+  int current_item = 0;
+#endif
 
 #if HAVE_HDF5_18
   group_hid = H5Gopen (loc_id, name, H5P_DEFAULT);
@@ -1660,15 +1665,22 @@
 
 #if HAVE_HDF5_18
   subgroup_hid = H5Gopen (group_hid, name, H5P_DEFAULT);
+
+  if ( H5Gget_info (subgroup_hid, &subgroup_info) < 0 )
+    return false;
+
+  while (current_item < subgroup_info.nlinks
+         && (retval2 = H5Literate (subgroup_hid, H5_INDEX_NAME,
+                                   H5_ITER_INC, &current_item,
+                                   hdf5_read_next_data, &dsub)) > 0)
 #else
   subgroup_hid = H5Gopen (group_hid, name);
-#endif
   H5Gget_num_objs (subgroup_hid, &num_obj);
-  H5Gclose (subgroup_hid);
 
   while (current_item < static_cast<int> (num_obj)
          && (retval2 = H5Giterate (group_hid, name, &current_item,
                                    hdf5_read_next_data, &dsub)) > 0)
+#endif
     {
       octave_value t2 = dsub.tc;
 
@@ -1684,6 +1696,8 @@
 
     }
 
+  H5Gclose (subgroup_hid);
+
   if (retval2 >= 0)
     {
       map = m;
diff -r 87c3848cf3c0 libinterp/octave-value/ov-fcn-handle.cc
--- a/libinterp/octave-value/ov-fcn-handle.cc	Tue Jun 10 14:03:09 2014 -0700
+++ b/libinterp/octave-value/ov-fcn-handle.cc	Thu Jun 12 18:00:26 2014 +0200
@@ -1099,12 +1099,16 @@
 #if HAVE_HDF5_18
       H5Eget_auto (H5E_DEFAULT, &err_func, &err_func_data);
       H5Eset_auto (H5E_DEFAULT, 0, 0);
+
+      hid_t attr_id = H5Aopen_by_name (group_hid, ".", "SYMBOL_TABLE",
+                                       H5P_DEFAULT, H5P_DEFAULT);
 #else
       H5Eget_auto (&err_func, &err_func_data);
       H5Eset_auto (0, 0);
+
+      hid_t attr_id = H5Aopen_name (group_hid, "SYMBOL_TABLE");
 #endif
 
-      hid_t attr_id = H5Aopen_name (group_hid, "SYMBOL_TABLE");
 
       if (attr_id >= 0)
         {
@@ -1138,12 +1142,18 @@
         {
           hsize_t num_obj = 0;
 #if HAVE_HDF5_18
+          H5G_info_t data_info;
+
           data_hid = H5Gopen (group_hid, "symbol table", H5P_DEFAULT);
+  
+          if ( H5Gget_info (data_hid, &data_info) < 0 )
+            return NULL;
+            
+          num_obj = data_info.nlinks;
 #else
           data_hid = H5Gopen (group_hid, "symbol table");
+          H5Gget_num_objs (data_hid, &num_obj);
 #endif
-          H5Gget_num_objs (data_hid, &num_obj);
-          H5Gclose (data_hid);
 
           if (num_obj != static_cast<hsize_t>(len))
             {
@@ -1154,11 +1164,20 @@
           if (! error_state)
             {
               hdf5_callback_data dsub;
+#if HAVE_HDF5_18
+              hsize_t current_item = 0;
+#else
               int current_item = 0;
+#endif
               for (octave_idx_type i = 0; i < len; i++)
                 {
+#if HAVE_HDF5_18
+                  if (H5Literate (data_hid, H5_INDEX_NAME, H5_ITER_INC, 
+                                  &current_item, hdf5_read_next_data, &dsub) <= 0)
+#else
                   if (H5Giterate (group_hid, "symbol table", &current_item,
                                   hdf5_read_next_data, &dsub) <= 0)
+#endif
                     {
                       error ("load: failed to load anonymous function handle");
                       success = false;
@@ -1168,6 +1187,9 @@
                   symbol_table::assign (dsub.name, dsub.tc, local_scope);
                 }
             }
+
+          H5Gclose (data_hid);
+
         }
 
       if (success)
@@ -1215,12 +1237,13 @@
 #if HAVE_HDF5_18
       H5Eget_auto (H5E_DEFAULT, &err_func, &err_func_data);
       H5Eset_auto (H5E_DEFAULT, 0, 0);
+      hid_t attr_id = H5Aopen (group_hid, "OCTAVEROOT", H5P_DEFAULT);
 #else
       H5Eget_auto (&err_func, &err_func_data);
       H5Eset_auto (0, 0);
+      hid_t attr_id = H5Aopen_name (group_hid, "OCTAVEROOT");
 #endif
 
-      hid_t attr_id = H5Aopen_name (group_hid, "OCTAVEROOT");
       if (attr_id >= 0)
         {
           H5Tclose (type_hid);
@@ -1249,7 +1272,11 @@
 
       if (success)
         {
+#ifdef HAVE_HDF5_18
+          attr_id = H5Aopen (group_hid, "FILE", H5P_DEFAULT);
+#else
           attr_id = H5Aopen_name (group_hid, "FILE");
+#endif
           if (attr_id >= 0)
             {
               H5Tclose (type_hid);
diff -r 87c3848cf3c0 libinterp/octave-value/ov-struct.cc
--- a/libinterp/octave-value/ov-struct.cc	Tue Jun 10 14:03:09 2014 -0700
+++ b/libinterp/octave-value/ov-struct.cc	Thu Jun 12 18:00:26 2014 +0200
@@ -988,7 +988,14 @@
   hid_t data_hid = -1;
 
 #if HAVE_HDF5_18
-  data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
+  hid_t gcpl;
+  gcpl = H5Pcreate (H5P_GROUP_CREATE);
+  if ( H5Pset_link_creation_order( gcpl, H5P_CRT_ORDER_TRACKED | 
+                                         H5P_CRT_ORDER_INDEXED ) < 0 )
+    return false;
+
+  data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, gcpl, H5P_DEFAULT);
+  H5Pclose(gcpl);
 #else
   data_hid = H5Gcreate (loc_id, name, 0);
 #endif
@@ -1030,22 +1037,31 @@
 
   herr_t retval2 = 0;
   octave_map m (dim_vector (1, 1));
+#if HAVE_HDF5_18
+  hsize_t current_item = 0;
+  H5G_info_t group_info;
+  
+  hid_t group_id = H5Gopen (loc_id, name, H5P_DEFAULT);
+  
+  if ( H5Gget_info (group_id, &group_info) < 0 )
+    return NULL;
+
+  while (current_item < group_info.nlinks
+         && (retval2 = H5Literate (group_id, H5_INDEX_CRT_ORDER, H5_ITER_INC,
+                                   &current_item, hdf5_read_next_data, &dsub)) > 0)
+#else
   int current_item = 0;
   hsize_t num_obj = 0;
-#if HAVE_HDF5_18
-  hid_t group_id = H5Gopen (loc_id, name, H5P_DEFAULT);
-#else
+
   hid_t group_id = H5Gopen (loc_id, name);
-#endif
   H5Gget_num_objs (group_id, &num_obj);
-  H5Gclose (group_id);
-
-  // FIXME: fields appear to be sorted alphabetically on loading.
-  // Why is that happening?
-
+
+  // fields will be sorted alphabetically on loading
+  // because tracking of creation order is not possible in HDF API v16
   while (current_item < static_cast<int> (num_obj)
          && (retval2 = H5Giterate (loc_id, name, &current_item,
                                    hdf5_read_next_data, &dsub)) > 0)
+#endif
     {
       octave_value t2 = dsub.tc;
 
@@ -1061,6 +1077,7 @@
 
     }
 
+  H5Gclose (group_id);
   if (retval2 >= 0)
     {
       map = m;
@@ -1625,9 +1642,17 @@
   hid_t data_hid = -1;
 
 #if HAVE_HDF5_18
-  data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
+  hid_t gcpl;
+  gcpl = H5Pcreate (H5P_GROUP_CREATE);
+  if ( H5Pset_link_creation_order( gcpl, H5P_CRT_ORDER_TRACKED | 
+                                         H5P_CRT_ORDER_INDEXED ) < 0 )
+    return false;
+  data_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, gcpl, H5P_DEFAULT);
+  printf("indexed group name: %s\n",name);
+  H5Pclose(gcpl);
 #else
   data_hid = H5Gcreate (loc_id, name, 0);
+  printf("Old HDF!!!\n");
 #endif
   if (data_hid < 0) return false;
 
@@ -1667,22 +1692,35 @@
 
   herr_t retval2 = 0;
   octave_scalar_map m;
+#if HAVE_HDF5_18
+  hsize_t current_item = 0;
+  H5G_info_t group_info;
+
+  hid_t group_id = H5Gopen (loc_id, name, H5P_DEFAULT);
+
+  if ( H5Gget_info (group_id, &group_info) < 0 )
+    return false;
+
+  while (current_item < group_info.nlinks
+         && (retval2 = H5Literate (group_id, H5_INDEX_CRT_ORDER,
+                                   H5_ITER_INC, &current_item,
+                                   hdf5_read_next_data, &dsub)) > 0)
+#else
   int current_item = 0;
   hsize_t num_obj = 0;
-#if HAVE_HDF5_18
-  hid_t group_id = H5Gopen (loc_id, name, H5P_DEFAULT);
-#else
+
   hid_t group_id = H5Gopen (loc_id, name);
-#endif
   H5Gget_num_objs (group_id, &num_obj);
-  H5Gclose (group_id);
-
-  // FIXME: fields appear to be sorted alphabetically on loading.
-  // Why is that happening?
+
+  // fields will be sorted alphabetically on loading
+  // because tracking of creation order is not possible in HDF API v16
 
   while (current_item < static_cast<int> (num_obj)
          && (retval2 = H5Giterate (loc_id, name, &current_item,
                                    hdf5_read_next_data, &dsub)) > 0)
+#endif
+
+
     {
       octave_value t2 = dsub.tc;
 
@@ -1696,6 +1734,7 @@
 
     }
 
+  H5Gclose (group_id);
   if (retval2 >= 0)
     {
       map = m;
