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

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 2K views
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
 
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.