Mubeen Shahid
Mubeen Shahid

Reputation: 316

static library from Fortran Modules

I am not a very expert Fortran programmer, but now that I have written many subroutines (in Fortran 90), I have to put them in Modules (employed by "use" statement in other subroutines and program) to avoid writing the interfaces. I have to use these modules with old code written in F77. I don't want the compiler to compile these modules again and again. So I created a static library from the ".o" files after compiling these modules:

ar rc libmymath.a module1.o module2.o module3.o

However I still need to keep the ".mod" files of these modules to be able to "use" them in my code.

My question: is it possible to pack these ".mod" files in the static library archive ".a" (as we did with .o files), so that everything is encapsulated in the single file static library?

P.S: by anywhere I mean across my systems, all of them use gfortran 64 bit.

Upvotes: 9

Views: 7758

Answers (3)

Danian Oliveira
Danian Oliveira

Reputation: 31

A simple way is to compile with

gfortran myprog.f90 -I/path/to/mod_files -L /path/to/lib -lmylib

where the module.mod are in the directory /path/to/mod_files. The module.o was generated by

gfortran -c /path/to/mod_files/module.f90

and the library mylib.a was generated by

ar rcv /path/to/lib/mylib.a /path/to/mod_files/module.o

But you still have to keep the .mod files.

I had this same issue.

I hope I have helped.

Upvotes: 3

Biplab Bijay
Biplab Bijay

Reputation: 31

You just copy those .mod files to your fortran finclude directory.

e.g I am using ubuntu with gcc -4.4.3. What i did is I have copied the library librandom.a to /usr/local/lib and the mod file random.mod to /usr/lib/gcc/i486-linux-gnu/finclude.

Now I don't have to create those mods again and again . I just have to use gfortran -o myfile myfile.f90 -lrandom to compile my program and link with the library. Of course i have to use "use random " in my myfile.f90.

Cheers

Upvotes: 3

tpg2114
tpg2114

Reputation: 15100

No, it is not possible.

In an analogue to C/C++, a .mod file is like a header file. It describes the contents of the module and the USE <module> is similar to the #include <header>.

These mod files are compiler (and often even version) specific and are required because modules name-mangle the functions and so there needs to be a lookup table for the resulting function names.

Upvotes: 14

Related Questions