I am trying to create a folder that contains all the modules I may use. Rather than compiling those modules whenever I need them, I pre-compiled the modules within that folder and would like to use the -I compiler flag to point to the pre-compiled modules (referenced in the gfortran manual). Despite that, I get a compiler (linker?) error that the module cannot be found. For example, consider this directory structure:
project_folder/
├─ src/
│ ├─ main.f90
├─ modules/
│ ├─ my_mod.f90
main.f90:
program main
use my_mod, only : f
implicit none
print *, f(20)
end program main
my_mod.f90:
module my_mod
implicit none
private
public :: f
contains
function f(arg) result(retval)
integer, intent(in) :: arg
integer :: retval
retval = arg**2
end function f
end module my_mod
Within the modules directory I compiled my_mod using
gfortran -c my_mod.f90
And within the src directory I tried to compile main using
gfortran -I../modules main.f90
When I try the src compilation I get the following error:
/usr/bin/ld: /tmp/cc82F9xs.o: in function `MAIN__':
main.f90:(.text+0x51): undefined reference to `__my_mod_MOD_f'
collect2: error: ld returned 1 exit status
How can I properly tell gfortran where the modules are so I can use pre-compiled modules?