Hello everyone! I am using Debian 12 (amd64) with gfortran 12.2 and gmake. I want to be able to create a shared library and then be able to link to this library to a new fortran program which is located in a directory elsewhere. I have been able to create the shared library and associated modules but I am having trouble calling the library’s subroutines and functions from the new program. Here are the steps I followed:
- Create and Compile Shared Library
One of the files in this library is constants.f03:
module dnx_constants
use iso_fortran_env, only: sp => real32, dp => real64
implicit none
real(dp), parameter :: dnx_pi=3.14159265358979323846d0
end module dnx_constants
Another file is vector.f03 and it is basically a module which contains a few subroutines:
module dnx_vector
use dnx_constants
implicit none
public :: dnx_vprint
contains
subroutine dnx_vprint(v, vname)
implicit none
integer :: i, n
real(dp), intent(in), dimension(:) :: v
character(len=8), intent(in) :: vname
n= size(v)
write(*,*)
write(*,*) vname
do i=1,n,1
print*, v(i)
end do
write(*,*)
end subroutine dnx_vprint
end module dnx_vector ! [EDIT] added missing line
I compiled the library as follows:
gfortran -c -std=f2008 -fPIC constants.f03 vector.f03
gfortran -shared constants.o vector.o -o libdnx.so
Compiles fine with no issues. Generates the .so as well as a couple .mod files.
- Creating and compiling a new program which uses the library (main.f03):
program main
use dnx_constants
use dnx_vector
implicit none
print*, dnx_pi
! call dnx_vprint((/1.d0, 1.d0, dnx_pi/),"v= ")
end program main
Compilation and linking are as follows
LIBDIR= /path/to/library
gfortran -L$(LIBDIR) -I$(LIBDIR) -ldnx main.f03 -o main.run
LD_LIBRARY_PATH environmental variable points to LIBDIR. Program compiles fine and it prints out the value of dnx_pi to the screen. Uncommenting the call to dnx_vprint gives me this error during compilation however:
/usr/bin/ld: /tmp/ccPeSQlf.o: in function `MAIN__':
main.f03:(.text+0x101): undefined reference to `__dnx_vector_MOD_dnx_vprint'
Any help would be appreciated.