I want to compile some of my often-used Fortran code into a static library (assuming I would have access to both *.mod and *.a files).
This worked.
However, I have encountered the problem with linking.
Here is an example. Let's say I have mylib.f90 file:
MODULE MYLIB
  IMPLICIT NONE
CONTAINS
  INTEGER FUNCTION FOO(N)
    INTEGER, INTENT(IN) :: N
    FOO = N + 2
  END FUNCTION FOO
END MODULE MYLIB
Which is compiled as
gfortran -c mylib.f90
ar rcs libmylib.a mylib.o
Now, I have a program that uses FOO function:
PROGRAM MYPROG
  USE MYLIB
  IMPLICIT NONE
  INTEGER M
  M = FOO(3)
  WRITE (*,*) M
END PROGRAM MYPROG
For simplicity I put it in the same directory as mylib.mod and libmylib.a. Compilation:
gfortran -c myprog.f90
no issues. Linking, however results in error:
gfortran -L./ -lmylib myprog.o
myprog.o: In function `MAIN__': myprog.f90:(.text+0x11): undefined
reference to `__mylib_MOD_foo' collect2: error: ld returned 1 exit
status
However, symbol __mylib_MOD_foo is in libmylib.a:
nm libmylib.a 
mylib.o:
0000000000000000 T __mylib_MOD_foo
and it compiles without a problem if I give it mylib.o. What am I doing wrong?
PS. I've seen somewhere, but can't find where now the way to link as follows:
gfortran -Wl,--whole-archive libmylib.a -Wl,--no-whole-archive myprog.o
It works. But it should solve the problem with weak symbols, whereas the output of nm didn't mark __mylib_MOD_foo as weak.
