我对要编译和执行一些具有HDF5依赖关系的代码有特定的要求。
我不想使用hdf5 compiler,但是我想编译HDF5源代码。

我对如何将HDF5链接到我的C程序非常陌生。请您详细说明如何执行此操作,以便我可以使用c编译器并链接从here下载的源文件来执行此程序。

示例C程序-

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from [email protected].     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 *  This example illustrates how to create a dataset that is a 4 x 6
 *  array.  It is used in the HDF5 Tutorial.
 */

#include "hdf5.h"
#define FILE "dset.h5"

int main() {

   hid_t       file_id, dataset_id, dataspace_id;  /* identifiers */
   hsize_t     dims[2];
   herr_t      status;

   /* Create a new file using default properties. */
   file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

   /* Create the data space for the dataset. */
   dims[0] = 4;
   dims[1] = 6;
   dataspace_id = H5Screate_simple(2, dims, NULL);

   /* Create the dataset. */
   dataset_id = H5Dcreate2(file_id, "/dset", H5T_STD_I32BE, dataspace_id,
                          H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

   /* End access to the dataset and release resources used by it. */
   status = H5Dclose(dataset_id);

   /* Terminate access to the data space. */
   status = H5Sclose(dataspace_id);

   /* Close the file. */
   status = H5Fclose(file_id);
}

最佳答案

为了进行编译,必须使-I标志指向HDF5的include目录。对于系统安装,通常为/usr/include,但根据以串行或并行方式,32/64位等方式安装HDF5,会有很多变化。对于链接-L-l标志是重要的。 -L应该指向包含HDF5库的.so.dll.dylib文件的目录(同样,可能会有变化),而-l只是给出了库名,-lhdf5和其他名称(我相信-lz-lm几乎总是使用)。如果使用高级库,则需要-lhdf5_hl

检查这些标志的最简单方法是调用

h5cc -show

它将列出所有这些。

PS:您可以在一个步骤中进行编译和链接(从.c到可执行文件),也可以先编译(从.c.o),然后链接(从.o到可执行文件)。在第一种情况下,需要所有-I-L-l标志。

关于c++ - 如何用HDF5源代码编译C程序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43151312/

10-13 08:03