Can‘t call a subroutine name in a subroutine in Fortran90

Click For Summary
SUMMARY

The forum discussion centers on a Fortran90 programming issue where a segmentation fault occurs when calling a subroutine. The user attempts to call subroutine B from within subroutine CaoB, but encounters a SIGSEGV error. The problem is resolved by moving subroutine B outside of the 'contains' section, allowing it to be recognized correctly. The user tested the code with Intel Fortran Compiler (ifort) version 2021.4.0 and GNU Fortran (GCC) version 10.2.0, confirming that the issue was related to the placement of the subroutine.

PREREQUISITES
  • Understanding of Fortran90 programming structure
  • Familiarity with subroutine and interface concepts in Fortran
  • Knowledge of compilation and execution processes in Fortran
  • Experience with debugging segmentation faults
NEXT STEPS
  • Research Fortran90 subroutine visibility and scope rules
  • Learn about the 'contains' statement and its implications in Fortran
  • Explore debugging techniques for segmentation faults in Fortran
  • Investigate differences between Intel Fortran Compiler and GNU Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, programmers troubleshooting subroutine calls, and anyone seeking to understand the nuances of Fortran90's structure and error handling.

pilipili2021
Messages
5
Reaction score
0
Homework Statement
Coding by ifort
Try to call a uncertain name subroutine
Relevant Equations
NONE
Here is the code for example:
Fortran:
PROGRAM  main
  CALL B()
  CALL CaoB(B)
  
  contains
   
  subroutine B()
  IMPLICIT NONE
  write (*,*) 'fk'
  end subroutine B

 subroutine CaoB(fcn)
 implicit none

 INTERFACE
   SUBROUTINE fcn()
     IMPLICIT NONE
   END SUBROUTINE fcn
 END INTERFACE

 write (*,*) '1'
 call B() 
 write (*,*) '2'
 call fcn()
 write (*,*) '3'

 end subroutine CaoB 

END
Then I compile it.

ifort main.F90

It seems no error. Then run it.

./a.outIt show that

fk
1
fk
2
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
a.out 000000000040492A Unknown Unknown Unknown
libpthread-2.31.s 00007F15A65153C0 Unknown Unknown Unknown
Unknown 00007FFFC18B82C0 Unknown Unknown Unknown

Surely there is something wrong when I call the subroutine fcn, which it should be named as B.

So, what should I do? what's wrong? Please let me know if any of you have any ideas or any comments.

thank you
 
Physics news on Phys.org
pilipili2021 said:
which it should be named as B
How does the main program know that ?
[edit] Or rather: the calling program, in this case CaoB

##\ ##
 
Last edited:
BvU said:
How does the main program know that ?

##\ ##
Thank you for your reply.

Do you mean add some code like 'external B' or 'external fcn'? That not works.

In the subroutine 'CaoB' it have a subroutine name 'fcn' determined by the interface. When I call 'CaoB(B)', 'B' as a subroutine name should be instead of 'fcn' in the subroutine 'CaoB'. It should be, but something goes wrong.
 
Your program appears to be fine. I have checked both with GNU Fortran (GCC) 10.2.0 and ifort version 2021.2.0 20210228.
 
Thank you for your reply!
It's a great help.
May be my environment have some problems.
my ifort version is 2021.4.0

So confused.
 
pilipili2021 said:
Thank you for your reply!
It's a great help.
May be my environment have some problems.
my ifort version is 2021.4.0

So confused.
Problems are usually fixed with newer versions :smile:.

By the way, my ifort is on Linux.
 
DrClaude said:
Problems are usually fixed with newer versions :smile:.

By the way, my ifort is on Linux.
My ifort is on ubuntu 20.04 , also on Linux.
Everything worked well before my hard disk suddenly broken.
May problems created with newer versions:cry:
 
finally I find the problem.

If I code like

Call CaoB()
contains
subroutine B()

the error comes.

If I move subroutine B out of 'contains' like

subroutine B()

subroutine something
call CaoB()
end subroutine something

Then the problem is solved.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
8K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 16 ·
Replies
16
Views
4K