Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

How to link two Fortran files

Agnes_Y_
Beginner
755 Views

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!

Regards,
Agnes 

0 Kudos
3 Replies
Anthony_Richards
New Contributor I
755 Views

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.

0 Kudos
Les_Neilson
Valued Contributor II
755 Views

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.

Les

0 Kudos
Agnes_Y_
Beginner
755 Views

Thanks! The suggestions are very useful!

0 Kudos
Reply