Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modules in fortran 90 1

Status
Not open for further replies.

FloatingFeather

Programmer
Apr 17, 2017
18
0
0
AR
I am using a subroutine in a code I have written, which used modules. Something weird happens when I compile the code with the module that corresponds to this subroutine. Actually, the whole subroutine is called as a module (it is an open access subroutine). The thing is that, in order to compile properly the whole module must be at the beginning of the code, otherwise I get the following error when attempting to compile with gfortran:

use gaussian
1
Fatal Error: Can't open module file ‘gaussian.mod’ for reading at (1): No such file or directory
compilation terminated.


However, if the module is at the beginning of the code it compiles ok. But it is impractical for coding, because I like to have my main code at the beginning of the file. However, once compiled, it doesn't matter if I move the whole module (subroutine) to the end, while I keep the compiled "gaussian.mod" file in the directory.

Is there a way to fix this problem? I mean, it is clear that I can keep the mod file, or even move the bunch of code that does that thing, but I would prefer something more elegant to do the job.
 
The easiest way is to run the build twice.

Alternatively, make it hierarchical so that the lower modules do not call the upper modules. Then compile in that order.
 
Run the build - that will create some of the mod files. Then run the build again to get the system to build.
 
Hi. Thank you again. I'm not familiar with the terminology you are using, sorry for that. I actually don't know what "running the build" means. I compile my code in a linux system using gfortran, basically I type at the command line something like "gfortran mycode.f90 -o mycompiledcode.x". You mean I should this twice? because when I do it the first time I get the error I've posted before, unless I already have the gauss.mod file in the directory where I'm compiling the code.
 
If you are using a makefile, then just run make twice.
If you are building each object and then linking manually then you need to work out the hierarchy. Have a look at the use clauses. Say you have the following.

main.f90 - use aaa, use bbb
aaa.f90 - use ccc
bbb.f90
ccc.f90

This gives you a tree

Code:
main +- aaa
     |- bbb -- ccc
This means that the build order should be

ccc
aaa
bbb
main

It does not matter if aaa is done before bbb or vice versa. If you have
Code:
main +- aaa -- bbb
     |- bbb -- ccc
then it must be

ccc
bbb
aaa
main

This does not cater for mutually recursive modules. If you have mutually recursive modules then you need to break the recursion.
 
Ok. I think I'm not doing things so correctly, and my level of understanding of the compilation procedure is just superficial (I know the compiler just translate my Fortran code to machine code, that's all it does for me). I'm not familiar with what you are saying about trees, but I understand that there is a hierarchy of dependencies. The way I was doing it was just copying the whole module inside my code, and then using it. That's how the problem appeared.

I'm not using makefiles, I've never get used to do it, but I know it is probably the best way to work. I just write a bunch of code of ten thousand lines, and compile it. It's probably not the best way to work, but I got used to it, and it gets the job done, but maybe I should study more that sort of thing.

Do you have any reference where I can read about all this?

Thanks a lot!
 
For makefiles, have a look at


These are just basic tutorials: not references. Get the makefile manual from the internet if you want a reference. Doesn't take very long to learn the basics. About a day and you'll be using the macros. OS scripts inside makefiles takes a bit longer. Just remember that the indents are tabs: not spaces.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top