0

I am trying to compile a Fortran 90 code that uses HDF5. For that purpose, I am using the following 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

But, I get the following error:

$ 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

I have no idea what the error is. Probably, there is something wrong with my Makefile.

Guido
  • 3
  • 3
  • Welcome, be sure to read the [tour] and [ask]. For all Fortran questions use tag [tag:fortran]. Fortran 90 is just one version. File .f90 does not mean Fortran 90, but free-form file of any version but the exact version is completely unimportant without the code. – Vladimir F Героям слава Mar 30 '18 at 06:13
  • To understand what *Undefined reference* is please see [tag:undefined-reference], it is very important to know that. – Vladimir F Героям слава Mar 30 '18 at 06:19
  • Don't you need `-lz` also? – Ross Mar 30 '18 at 15:32
  • 1
    Hey all, I believe that this is not a duplicate. Using HDF5 is a typical "Fortran computing" use case and the way the question is presented here is more likely to pop up in google searches than the suggested duplicate. – Pierre de Buyl Mar 30 '18 at 20:22
  • This is not a duplicated question! The question marked as the answer is too general and does not provide the answer, it just gives possible reasons causing the problem. – Guido Apr 03 '18 at 22:42

1 Answers1

1

To compile Fortran code with HDF5 enabled, you can replace

FC        = gfortran

by

FC        = h5fc

and skip all the hdf5 flags, as the h5fc wrapper will take care of those.

If you have some specific reason of calling the compiler by its name, you can learn about what flags are needed by calling

h5fc -show

that will show you what flags are added to the compiler.

On my computer (linux with gfortran), the result is:

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

Often you can get by with less flags than that, which you can find with some experimentation.

Given the error message you report, you lack the -ldl flags that enables linking of the dynamic linking library, see for isntance this other SO question.

Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
  • Sorry, but there's no way I want to use a custom wrapper just for including hdf5. The `h5fc -show` is useful, but (in my opinion) only for diagnosing the original problem. – Ross Mar 30 '18 at 15:33
  • I personally use cmake and the FindHDF5 utility. For small projects, I use h5fc and see no issue with it. – Pierre de Buyl Mar 30 '18 at 20:24
  • Thank you Pierre, using the output of h5fc -show in the variable FORTRANLIB solved the problem. – Guido Apr 03 '18 at 22:44