'"undefined reference" error when trying to compile a fortran program with external dependencies using ifort

I am using the following command to compile my fortran program, which is the last line of the result of the hdf5 h5fc command on my Ubuntu 20.04.4 LTS x86_64 machine (added newlines for legibility):

<path_to_my_intel_oneapi_folder>/compiler/2022.0.2/linux/bin/intel64/ifort -shlib 
-I<path_to_my_hdf5_1.12.2>/include 
-L<path_to_my_hdf5_1.12.2>/lib 
-lhdf5_fortran 
-lhdf5 
-I<path_to_my_mpich_4.0.2>/include 
-L<path_to_my_mpich_4.0.2>/lib 
-lmpifort -Wl,-rpath -Wl,<path_to_my_mpich_4.0.2>/lib 
-lmpi <path_to_my_intel_oneapi_folder>/compiler/2022.0.2/linux/bin/intel64/ifort

I built hdf5 with cmake instead of autotools, which has slight differences in the resulting installation but should still work on my current machine. Linking after successfully compiling results in a large number of undefined reference errors, such as following:

ld: /path_to_my_file/my_file.o: in function `hdf5_utilities_mp_hdf5_openfile_':
my_file.f90:(.text+0xcbf8): undefined reference to `h5global_mp_h5p_file_access_f_'
ld: my_file.f90:(.text+0xcc0a): undefined reference to `h5p_mp_h5pcreate_f_'
ld: my_file.f90:(.text+0xcc2a): undefined reference to `h5global_mp_h5f_acc_trunc_f_'
ld: my_file.f90:(.text+0xcc45): undefined reference to `h5f_mp_h5fcreate_f_'
ld: my_file.f90:(.text+0xcc63): undefined reference to `h5global_mp_h5f_acc_rdwr_f_'
ld: my_file.f90:(.text+0xcc79): undefined reference to `h5f_mp_h5fopen_f_'
ld: my_file.f90:(.text+0xcc93): undefined reference to `h5global_mp_h5f_acc_rdonly_f_'
ld: my_file.f90:(.text+0xcca9): undefined reference to `h5f_mp_h5fopen_f_'
ld: my_file.f90:(.text+0xccc4): undefined reference to `h5p_mp_h5pclose_f_'

The path <path_to_my_hdf5_1.12.2>/lib, which is specified in the -L flag of the abovementioned ifort command, contains the files libhdf5_fortran.so.200.1.1 and libhdf5_fortran.a which contain the references to the hdf functions that cannot be linked, such as h5global_mp_h5p_file_access_f_. I assume that those errors are related to the following command, which is located in a different file that my_file.f90 makes use of:

module HDF5_utilities
  use HDF5
...
integer(HID_T) function HDF5_openFile(fileName,mode,parallel)
  ...
  integer(HID_T) :: plist_id
  integer :: hdferr
  call H5Pcreate_f(H5P_FILE_ACCESS_F, plist_id, hdferr)
  ...

This problem has started occuring since I created a new ifort hdf5 setup on my machine and my source files had no issues compiling prior to this, so I assume this is related to the way hdf5 is currently installed on my system. I installed hdf5 with cmake and used mpich as a dependency and am am using the spack package manager to install those packages. hdf5 is fetched from the tar.gz file from this source and compiled using the intel compiler from the intel oneapi package.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source