Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to run FORTRAN file with too many subroutine and function?

175 views
Skip to first unread message

ali_n...@yahoo.com

unread,
Jan 23, 2021, 3:01:44 PM1/23/21
to
The main program with too many subroutines and functions is existed. Some of the subroutines and functions also use subroutines, functions, and modules. I decided to write them in independent files due to clarity in the main program. Then I created an independent file as a module to collect them with the include command.

Now I have one main program and one independent file for the module. I compile the program but it shows an error. The error is about missing mentioned subroutine in the above independent file.

My question in general is: If you have a program with one main program and a number of sub-programs (subroutines, functions, and modules), which also they have sub-programs, what method do you use to run it?

I write my code in PLATO as a fortran95.

I will appreciate any comments that give me a little help and forgive me for writing shortcomings

Jos Bergervoet

unread,
Jan 23, 2021, 6:20:04 PM1/23/21
to
On 21/01/23 9:01 PM, ali_n...@yahoo.com wrote:
> The main program with too many subroutines and functions is existed. Some of the subroutines and functions also use subroutines, functions, and modules. I decided to write them in independent files due to clarity in the main program. Then I created an independent file as a module to collect them with the include command.
>
> Now I have one main program and one independent file for the module. I compile the program but it shows an error. The error is about missing mentioned subroutine in the above independent file.
>
> My question in general is: If you have a program with one main program and a number of sub-programs (subroutines, functions, and modules), which also they have sub-programs, what method do you use to run it?

Your situation is what most programs in general will present.
I prefer to have all functions and subroutines grouped into
modules. To run, this would then require something like:

$> g95 -o prog this_mod.f90 that_mod.f90 other-mod.f90 main.f90
$> ./prog argument1 argument2 ...

If you do not want to construct the first line (with the
modules all in the right order before the main program) by
hand, then you can use some 'make file' tooling, or some GUI
that does those things (but I never use the latter). Finding
the dependencies between the modules can be tricky, but as
long as there are only a dozen or so, a few trials and errors
can still solve it.

And if you do not have the situation where all is encapsulated
into modules, you could of course add the files with those
"lonely" functions and subroutines to the compile line with
the modules and the main program. But often this means that
those functions and subroutines would then need an interface
added to the main program, and/or the modules that use them,
for proper functioning. It usually pays of (and it keeps the
other files simpler) to avoid lonely routines and put them
in one or more modules, where the interface is then defined
just once.

On the other hand, for historical reasons one might want to
keep some functions (like Lapack or BLAS routines) exactly
as they are provided.. Having a few all-Caps fixed-format
Fortran 77 files in the list has its charm, of course!

--
Jos

jfh

unread,
Jan 24, 2021, 1:48:29 AM1/24/21
to
> --
> JosI

Jos makes me wonder whether

gfortran -o prog this_mod.f90 that_mod.f90 other_mod.f90 main.f90

is any better than putting at the beginning of main.f90

include 'this_mod.f90'
include 'that_mod.f90'
include 'other_mod.f90'

and just compiling with

gfortran -o prog main.f90

which is what I would usually do if all the files have the same source form.

ali_n...@yahoo.com

unread,
Jan 24, 2021, 3:24:32 AM1/24/21
to
Thank you for your answer.
I also see in some program that uses:
> $> g95 -o prog this_mod.f90 that_mod.f90 other-mod.f90 main.f90
> $> ./prog argument1 argument2 ...
But this is my first pro-code so I don't know what is it and how I can write it.
Can you provide some keywords or links that help me what is the above line

Beliavsky

unread,
Jan 24, 2021, 8:59:32 AM1/24/21
to
It is better not to use include, since you may want to recompile the main program with recompiling everything else.

Jos Bergervoet

unread,
Jan 26, 2021, 6:02:18 AM1/26/21
to
I see.. You will have to install some Fortran compiler system
anyway, and that will usually come with an example of how to run
a program, probably also explaining the case when it is a main
program together with one or more other files.

If that information is missing, we cannot really provide it from
here without more information, it really depends on the operating
system and the compiler. The above, for instance, would hold if
the system is Linux and the compiler g95 (but that was just to
keep the example lines shorter than when it is gfortran!)

Perhaps people here can help if you first show what you already
can do (probably some "hello world" program will work) and what
commands you are using to do that. Looking on the web for more
documentation might also be a (perhaps even faster) way..

--
Jos
0 new messages