Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

fortran - Function call stopping/hanging when containing a write-statement, but only when linking with certain libraries during compilation

Here is my minimal example:

program test    
  implicit none
  real :: testfunc
  write(*,*) "Writing from main"
  write(*,*) testfunc()
end program test

function testfunc() result(y)
  real             :: y
  write(*,*) "Write from function g"
  y=1.0
  return
end function testfunc

When compiling with a simple

gfortran test.f90

or when including a library like Slatec

gfortran test.f90 -lslatec

It works fine.

However, when changing the library to -llapack of -lblas, then the program hangs at runtime when calling testfunc(). Note that my example code here does not actually use any of these libraries. The last thing i see is "Writing from main", then nothing happens, and i have to CTRL-C to regain control. When hanging, the process does not appear to be using any CPU cycles.

The strange thing now is that, if i comment out the write statement inside testfunc(), it works all the time.

So my question is really: Can these libraries really prevent me from printing inside my own functions? Why? How?

(I'm actually working on a larger program which needs lapack and blas, so i obviously can't just stop linking to them.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As far as I remember, it is not standard conforming to call recursively the WRITE keyword.

To correct you program, modify slightly your main program

program test    
  implicit none
  real :: testfunc,result
  write(*,*) "Writing from main"
  result=testfunc()
  write(*,*) result
end program test

From my point of new, the trouble you met has therefore nothing to do with the used libraries but the symptoms of the mistake may change in that case (from apparently no bug to a crash).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...