使用 HDF5 的 Fortran 代码的 makefile 中的未定义引用错误

Undefined reference error from a makefile for a Fortran code with HDF5

我正在尝试编译使用 HDF5 的 Fortran 90 代码。为此,我使用以下 Makefile:

# Location of HDF5 binaries (with include/ and lib/ underneath)
HDF5 = /fs/posgrado16/other0/guido/libraries/hdf5/serial
# Compiler
FC        = gfortran
# ------ No machine-specific paths/variables after this  -----
FORTRANLIB=-I$(HDF5)/include $(HDF5)/lib/libhdf5_fortran.a

FSOURCE = h5_crtgrpar.f90
OBJECTS = $(FSOURCE:.f90=.o)
EXECUTABLE = $(FSOURCE:.f90=.exe)
LIBSHDF = $(FORTRANLIB) $(HDF5)/lib/libhdf5.a


all:$(EXECUTABLE)


$(EXECUTABLE):$(OBJECTS)
    $(FC) -o $@ $^ $(LIBSHDF)

$(OBJECTS):$(FSOURCE)
    $(FC) -c $@ $< $(LIBSHDF)


.PHONY : clean

 clean:
    rm -f $(FSOURCE) $(OBJECTS) *.h5

但是,我收到以下错误:

$ make -f Makefilef 
gfortran -o h5_crtgrpar.exe h5_crtgrpar.o -I/fs/posgrado16/other0/guido       /libraries/hdf5/serial/include /fs/posgrado16/other0/guido/libraries/hdf5/serial/lib/libhdf5_fortran.a /fs/posgrado16/other0/guido/libraries /hdf5/serial/lib/libhdf5.a 
/fs/posgrado16/other0/guido/libraries/hdf5/serial/lib/libhdf5.a(H5PL.o): In    function `H5PL_term_interface':
H5PL.c:(.text+0x205): undefined reference to `dlclose'
/fs/posgrado16/other0/guido/libraries/hdf5/serial/lib/libhdf5.a(H5PL.o): In   function `H5PL_load':
H5PL.c:(.text+0x477): undefined reference to `dlsym'
H5PL.c:(.text+0x5be): undefined reference to `dlopen'
H5PL.c:(.text+0x5d7): undefined reference to `dlsym'
H5PL.c:(.text+0x704): undefined reference to `dlclose'
H5PL.c:(.text+0x789): undefined reference to `dlerror'
H5PL.c:(.text+0x960): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
make: *** [h5_crtgrpar.exe] Error 1

我不知道错误是什么。可能是我的 Makefile 有问题。

要在启用 HDF5 的情况下编译 Fortran 代码,您可以替换

FC        = gfortran

来自

FC        = h5fc

并跳过所有 hdf5 标志,因为 h5fc 包装器会处理这些。

如果您出于某种特定原因需要通过名称调用编译器,您可以通过调用

了解需要哪些标志
h5fc -show

这将向您显示添加到编译器的标志。

在我的电脑上(linux 使用 gfortran),结果是:

gfortran -g -O2 -fstack-protector-strong -I/usr/include/hdf5/serial -L/usr/lib/x86_64-linux-gnu/hdf5/serial /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5hl_fortran.a /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_hl.a /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_fortran.a /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5.a -Wl,-z,relro -lpthread -lz -ldl -lm -Wl,-rpath -Wl,/usr/lib/x86_64-linux-gnu/hdf5/serial

通常你可以使用比这更少的标志,你可以通过一些实验找到它。

鉴于您报告的错误消息,您缺少启用动态链接库链接的 -ldl 标志,请参阅其他 SO question.