如何使用HDF5源代码编译C程序?

3
我有一个具体的要求,需要编译和执行一些依赖于HDF5的代码。 我不想使用hdf5 compiler,而是想编译HDF5源代码。
我对如何将HDF5链接到我的C程序非常陌生。请详细解释如何做到这一点,以便我可以使用c编译器执行此程序并链接从这里下载的源文件。
示例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 help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 *  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);
}
1个回答

4
编译时需要使用-I标志指向HDF5包含目录。对于系统安装而言,通常是/usr/include,但会因为HDF5的串行或并行安装、32/64位等因素有很多变化。链接时需要使用-L-l标志。-L应该指向包含HDF5库的.so.dll.dylib文件的目录(同样可能有变化),-l只是给出库的名称,例如-lhdf5和其他一些库(我认为几乎总会用到-lz-lm)。如果使用高级别库,则需要使用-lhdf5_hl
检查这些标志的最简单方法是调用:
h5cc -show

这将列出所有它们。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接