Tags

, , , , , , , , ,

If you have main.f and as the main fortran 77 program and it calls upon subroutines in other files (say, files 1.f, 2.f,…. 7.f), you can compile as (we’ll use GNU Fortran compiler as an example)

$ gfortran main.f [1-7].f

But if you change only one of them (say main.f) and do not touch others at all, you can make a library out of the unchanging ones and link to the library during compilation:

First, create the library:

$ rm -f *.o
$ gfortran -c [1-7].f
$ ar -rcs libmylib.a [1-7].o

This will create the library libmylib. You may put it in convenient location (might I suggest /usr/local/lib/ directory?). You can list out the object files by using:
$ ar t libmylib.a

Next while compiling main.f, you can link to the library in following three ways:
$ gfortran main.f /path_to_the_library/libmylib.a -o executable
or
$ gfortran main.f -L/path_to_the_library/ -lmylib -o executable
or if the library is in a “well-define” library search path, e.g. /usr/local/lib, then
$ gfortran main.f -lmylib

Reference: Ubuntu forum.

Advertisements