Libraries – How to make them and how to use... Ambar K. Mitra, Aerospace Engineering, Iowa State University

advertisement
Libraries – How to make them and how to use them?
Ambar K. Mitra, Aerospace Engineering, Iowa State University
You are probably familiar with two kinds of procedures in FORTRAN –
(1) Subroutines and (2) Functions.
Subroutines are invoked by naming them in a CALL statement. Subroutines
can return multiple results through calling arguments.
Function subprograms are procedures invoked by naming them in an
expression. Functions can return a “single” result that is used in the
evaluation of the expression.
Advantages of subroutines and functions are:
(1)
(2)
Independent testing of subtasks.
Reusable code.
What is a Library
Imagine that you want to find average of an array (a set of numbers), in
our main program, we can write a sub-routine to find the average and
call the subroutine from the main program. For that, we usually append
this sub-routine code below the main program or as a separate
FORTRAN file and compile it with main program. Thus at each time we
either append or repeatedly compile this additional sub-routine.
Libraries eliminate the need of this repeated compilation. We can store
the pre-compiled sub-routines as an 'object file' in a Library, and
these object files can be called from anywhere, by linking these
libraries.
Library is an archive of a set of general sub-routines which can be
called/used/linked to any of our programs.
Consider the following scenario consisting of a main program and a
subroutine.
------------------------------------------------------------------------------------------------
program mymain_One
……………..
……………..
call mysub (inparam1, inparam2, outparam1, outparam2)
………………
end
subroutine mysub (v1, v2, v3, v4)
…………………
return
end
------------------------------------------------------------------------------------------------------Now, let us compile mysub and store it in a library so that it can be
called from any main program, e.g., mymain_One, mymain_Two,
etc.
Step-1
------------------------------------------------------------------------------------------------------Assign a name to mysub as mysub.F
subroutine mysub (v1, v2, v3, v4)
…………………..
return
end
------------------------------------------------------------------------------------------------
-----------step-2
Compile this sub-routine and make an object file. Here, we are using the
Intel compiler under UNIX/Linux
[myprompt]$ ifort -c mysub.F
this will give the object file average.o. Using the object file make a
library
[myprompt]$ ar r libmylib.a mysub.o
The command 'ar' is to make archives. This will give a library
(libmylib.a).
step-3
To link this library into your programs, use the following command
[myprompt]$ ifort mymain_One.F -L/path/ -lmylib
This step will compile the program mymain_One.F, and link it to the
library called 'libmylib.a'. The -L/path/ refers to the location of your
library. You can put it in a directory where the compiler will look when
the compilation takes place (e.g. /usr/lib /usr/local/lib etc.. ) In a similar
manner, you can make 'as many general subroutine as you want', and
make them object files and put it together in a library. It can be used
simply by linking those libraries on your compilation.
For multiple number of object files, create the library with
[myprompt]$ ifort -c mysub1.F mysub2.F mysub3.F
[myprompt]$ ls *.o
mysub1.o mysub2.o mysub3.o
[myprompt]$ ar r libmylib.a mysub1.o mysub2.o mysub3.o
To append an object file into an existing library is the same as above
myprompt]$ ar r libmylib.a mysub4.o
To get a list of the object files in a library, you can use
[name@xxxxx]$ ar t libmylib.a
mysub1.o
mysub2.o
mysub3.o
mysub4.o
These are many standard libraries available for FORTRAN programs.
For example, BLAS (Basic Linear Algebra Subprograms), LAPACK
(Linear Algebra Package) are popular libraries for FORTRAN programs.
CAUTION! If you combine your own library with other standard libraries
like BLAS or LAPACK, care must be taken that your variables are not
conflicting with the variables of other libraries. This may give erroneous
results. Moreover, a pre-compiled library for one machine architecture
may conflict with other machines, because byte compatibility is different
for different machines (like Sun, Alpha Linux, other Unix platforms)
Download