Re: creating a Fortran library

From: Richard E Maine (nospam_at_see.signature)
Date: 02/23/05


Date: Wed, 23 Feb 2005 07:52:46 -0800

In article <1109170430.427679.254230@z14g2000cwz.googlegroups.com>,
 "ajs" <ajs00g@gmail.com> wrote:

> $ g95 -L../lib -I../lib -lmylib myprog.f90
> /cygdrive/c/DOCUME~1/ajs/LOCALS~1/Temp/ccWNfKz4.o(.text+0x41):myprog.f90:
> undefined reference to `mymod_MP_f_'
>
> Then I tried this:
>
> ajs@DDXX2F51 ~/TEST/test
> $ g95 -L../lib -I../lib myprog.f90 -lmylib
>
> VOILA!

This isn't really Fortran-specific at all, much less module-specific.
It is just how the Unix linker works... for all languages. The
command line is very picky about the ordering of things. On occasion,
this can be used to advantage as a "feature". More often (in my
experience), it is an annoyance and a source of confusions like this.

Not that the usual Windows linkers are any better, but I used to be
"spoiled" by linkers on other systems that are no longer around.

Basically, think of the -l switch as telling the linker to search
the specified library... right now. In the original version above,
there isn't then anything that the linker knows is needed from the
library, as the main program hasn't been mentioned yet. After searching
the library, the main program is loaded. The main program has references
to the module, but by then it is too late - the library won't be
searched again.

You'll have similar problems if you have multiple libraries, some of
which reference other libraries (quite common in all but pretty small
programs). You then need to specify the libraries in an appropriate
order reflecting the dependencies.

-- 
Richard Maine                       |  Good judgment comes from experience;
email: my first.last at org.domain  |  experience comes from bad judgment.
org: nasa, domain: gov              |        -- Mark Twain


Relevant Pages