How to link two Fortran files

How to link two Fortran files

Hi everyone,

I am very new to Fortran and want to ask how can I run a main program in one Fortran files from another Fortran files. For instance, I have two Fortran files: a.for and b.for,

Inside a.for, there is a main program called xx

program xx
end program xx

Inside b.for , there is a main program called yy, all I want to is to call the main program xx inside programm yy:

 program yy
(Call the main program xx from a.for)

end  program yy

Do you know is it possible to do that? Thanks in advance!


4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

If you are very new to Fortran, then you need to investigate SUBPROGRAMS which can be called from a main program.
For a simple console program, you can only have one main program indicated by the PROGRAM statement.
You can split up your processing into SUBROUTINES and FUNCTIONS that can eilther be called (subroutines)  using Fortran statements
such as CALL MYSUBROUTINE(.....optional arguments...) or referenced (functions) using Myvalue = MYFUNCTION(...optional arguments...).

You choose your own subroutine and function names. The code for each can either be included in the same Fortran file as the main program or it can exist withing its own Fortran file. Just add all the files to the Fortran project and the compiler will compile them and link them together for you when commanded.

As you get more experienced, you can investigate creating a LIBRARY of subroutines and functions that you might use often and which you might want to access from seperate programs, in which case you include the library in your main Fortran project and the linker will automatically search the library for the names of routines and/or functions that you want to use in your main program and, when it finds them,it will include the code into your executable.

If you really do mean to "run" one program from another program then you should look at the help for SYSTEM.
There are other ways of doing it but this is one way.


Thanks! The suggestions are very useful!

Leave a Comment

Please sign in to add a comment. Not a member? Join today