For Programmers: Free Programming Magazines  


Home > Archive > Fortran > February 2005 > creating a Fortran library









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author creating a Fortran library
Bart Vandewoestyne

2005-02-23, 4:00 pm

Continuing my Fortran learning-process, I would like to try to create a
Fortran 90/95 library, so that others can easily use my code by linking against
it.

I've set up a simple test example that goes like this (suppose all
directories are relative to the root directory of the program):

Under xxx/lib/ i have a file mymod.f95 with the content:

module mymod

implicit none

contains

function f(x) result (y)
integer :: x
integer :: y

y = 2*x
end function f

end module mymod


xxx/lib/ might contain other modules for doing all kinds of stuff. The content
of all these modules should be put in a library that then can be called by
other programs.


Then under xxx/tests/ i would have programs that make use of my library, so
for example a program with content:

program myprog

use mymod

implicit none

integer :: x

print *, f(x)

end program myprog


I have been googling for a while now, but could not find a clear answer nor the
right Google-search-terms I could use to find an answer...

The best thing I could come up with (after some Googling) was:

Create my library with:

/lib$ g95 -c mymod.f95

/lib$ ar rcv libmylib.a mymod.o

/lib$ ranlib libmylib.a

Compile my program (and link with my library) as follows:

/tests$ g95 -L../lib -I../lib -lmylib myprog.f95

But this gives me the error:

/tmp/ccEldCuz.o: In function `MAIN_':
/tmp/ccEldCuz.o(.text+0x41): undefined reference to `mymod_MP_f_'

So apparently the function f(x) from my module is not found, although I do
link with my library???

Since I know this might be compiler-dependent: i have access to the g95
compiler and NagWare's f95 compiler, so a solution for either one of these
compilers is fine.


Regards,
Bart

--
"Share what you know. Learn what you don't."
ajs

2005-02-23, 4:00 pm

I repeated this test on my cygwin :
First i did exactly what you did :

$ 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!


ajs@DDXX2F51 ~/TEST/test
$ ./a.exe
8

AJS

Richard E Maine

2005-02-23, 4:00 pm

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
Bart Vandewoestyne

2005-02-23, 4:00 pm

In article <1109170430.427679.254230@z14g2000cwz.googlegroups.com>, ajs wrote:
> I repeated this test on my cygwin :
> First i did exactly what you did :
>
> $ 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!


Yes, before I've read your post, I also found my mistake after a lot of
playing around :-)

Regards,
Bart

--
"Share what you know. Learn what you don't."
Herman D. Knoble

2005-02-23, 4:00 pm

Bart: Arnaud Desitter's arg passing bench marks, Results section, of:
http://ftp.aset.psu.edu/pub/ger/for...ing/results.txt
See the Build options for each compiler which shows exactly how to
create, link, and use libraries in each case.

Skip


On Wed, 23 Feb 2005 13:55:44 +0000 (UTC), Bart Vandewoestyne
<MyFirstName.MyLastName@telenet.be> wrote:

-|Continuing my Fortran learning-process, I would like to try to create a
-|Fortran 90/95 library, so that others can easily use my code by linking against
-|it.
-|
-|I've set up a simple test example that goes like this (suppose all
-|directories are relative to the root directory of the program):
-|
-|Under xxx/lib/ i have a file mymod.f95 with the content:
-|
-|module mymod
-|
-|implicit none
-|
-|contains
-|
-| function f(x) result (y)
-| integer :: x
-| integer :: y
-|
-| y = 2*x
-| end function f
-|
-|end module mymod
-|
-|
-|xxx/lib/ might contain other modules for doing all kinds of stuff. The content
-|of all these modules should be put in a library that then can be called by
-|other programs.
-|
-|
-|Then under xxx/tests/ i would have programs that make use of my library, so
-|for example a program with content:
-|
-|program myprog
-|
-| use mymod
-|
-| implicit none
-|
-| integer :: x
-|
-| print *, f(x)
-|
-|end program myprog
-|
-|
-|I have been googling for a while now, but could not find a clear answer nor the
-|right Google-search-terms I could use to find an answer...
-|
-|The best thing I could come up with (after some Googling) was:
-|
-|Create my library with:
-|
-| /lib$ g95 -c mymod.f95
-|
-| /lib$ ar rcv libmylib.a mymod.o
-|
-| /lib$ ranlib libmylib.a
-|
-|Compile my program (and link with my library) as follows:
-|
-| /tests$ g95 -L../lib -I../lib -lmylib myprog.f95
-|
-|But this gives me the error:
-|
-|/tmp/ccEldCuz.o: In function `MAIN_':
-|/tmp/ccEldCuz.o(.text+0x41): undefined reference to `mymod_MP_f_'
-|
-|So apparently the function f(x) from my module is not found, although I do
-|link with my library???
-|
-|Since I know this might be compiler-dependent: i have access to the g95
-|compiler and NagWare's f95 compiler, so a solution for either one of these
-|compilers is fine.
-|
-|
-|Regards,
-|Bart

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com