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

AI Thread Summary
The discussion revolves around a Fortran90 code issue where a segmentation fault occurs when calling a subroutine. The user initially attempts to call subroutine B within another subroutine, CaoB, but encounters an error when trying to use an interface for function calls. It is suggested that the problem arises from the placement of subroutine B within the 'contains' section, which causes confusion in how the main program recognizes the subroutine names. Moving subroutine B outside of the 'contains' section resolves the issue, indicating that the structure of the code affects subroutine accessibility. The conversation highlights the importance of understanding subroutine scope and interface declarations in Fortran90 programming.
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
Views
2K
Replies
3
Views
4K
Replies
1
Views
3K
Replies
5
Views
8K
Replies
1
Views
2K
Replies
5
Views
3K
Replies
8
Views
4K
Replies
16
Views
3K
Back
Top