Fortran Fortran 77 - subroutine in separate file

Click For Summary
It is possible to save subroutines in a separate file and call them from a main program, allowing for more efficient code management and reuse. In the example provided, the main program 'TEST' can be split into 'test.f' and 'sub.f' for the subroutine. To compile both files together using gfortran, the command is straightforward: `gfortran test.f sub.f -o test.exe`, which recompiles the subroutine each time. For larger subroutines, it's more efficient to compile them separately into an object file using `gfortran sub.f -c`, which creates a 'sub.o' file. The main program can then be linked with this object file using `gfortran test.f sub.o -o test.exe`. This method improves compilation speed and allows for easier updates to the subroutine without recompiling the entire program.
jf22901
Messages
55
Reaction score
1
Hi all

I am currently using subroutines, and placing them at the end of the main program. However, I was wondering if it is possible to save them to a separate file, which the main program then calls?

For example, in the program below I call the subroutine 'SUB', which is located at the end of the program 'TEST'. Is there any way in which it could be split so that the program 'TEST is in a file called 'test.f' and the subroutine is in a file called 'sub.f'? That way I could call the same subroutines from different programmes, rather than having to put them at the end all the time.

Many thanks,

Jack

Code:
      PROGRAM TEST
      IMPLICIT NONE
      INTEGER X, Y, Z, TOTAL, TOT
      PRINT *, 'Enter numbers X,Y,Z where Y > X'
      READ *, X, Y, Z
      CALL SUB(X, Y, Z, TOTAL)
      PRINT *, 'TOTAL =', TOTAL
      END
C***********************************************************************
C Subroutine called above is located below
C***********************************************************************
      SUBROUTINE SUB(A, B, C, TOT)
      INTEGER TOT, A, B, C, I
      TOT=C
      DO 10 I=A,B
            TOT = TOT*I
10    END DO
      RETURN
      END
 
Last edited:
Technology news on Phys.org
I should add that to compile the above program (assuming I've called it 'test.f'), I would type at the command line:

ifort -C test.f -o test.exe
./test.exe

Jack
 
Try this:

ifort -C test.f sub.f -o test.exe

I have gfortran, not ifort, and I don't use the -C switch. For me, this works:

gfortran test.f sub.f -o test.exe

This recompiles the subroutine every time. If you have a really big subroutine, or a collection of subroutines in a single file, that takes a long time to compile, you can compile them separately and then link them in when you compile the main program. For me it would look like this:

gfortran sub.f -c

which produces an "object file" sub.o. Note that the switch is lowercase 'c' not uppercase 'C'. Then

gfortran test.f sub.o -o test.exe
gfortran foobar.f sub.o -o foobar.exe
etc.

The .o extension signals that the file doesn't need to be compiled, just linked into the compiled program.
 
Last edited:
Brilliant - works a treat. Thanks very much Jon.

However, I'm slightly worried that I tried it and it worked first time. That's not supposed to happen with programming is it?! :-p
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 59 ·
2
Replies
59
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K