我正在测试helloBessel
书中所写的 Fortran 90 vol2 中的 Numerical Recipies。
program helloBessel
use nrtype
use nr, ONLY: flmoon, bessj0
implicit none
integer(I4B) :: n = 200, nph = 2, jd
real(SP) :: x, frac, ans
call flmoon(n, nph, jd, frac)
write (*,*) jd
x = jd**0.25_sp
ans = bessj0(x)
write (*,*) "Hello, Bessel: ", ans
end program helloBessel
它给出了这个错误:
gfortran -o bessel nr.o nrtype.o helloBessel.f90
/tmp/cchJfVZl.o: In function `MAIN__':
helloBessel.f90:(.text+0x28): undefined reference to `flmoon_'
helloBessel.f90:(.text+0xb4): undefined reference to `bessj0_s_'
collect2: error: ld returned 1 exit status
我尝试制作较小的示例以防万一:
program helloTest
use nrtype
use nr, ONLY: test
implicit none
real(SP) :: z, t
call test(z, t)
write (*,*) t
end program helloTest
我是这样编译的:
gfortran -c nr.f90
gfortran -c nrtype.f90
gfortran -o test nr.o nrtype.o helloTest.f90
它仍然给出这个错误:
/tmp/cc76cg5g.o: In function `MAIN__':
helloTest.f90:(.text+0x1a): undefined reference to `test_'
collect2: error: ld returned 1 exit status
是代码问题还是编译过程有问题?我究竟做错了什么?我的问题与其他问题不同,因为我的版本足够高,不会引起问题,而且我已经告诉编译器所有文件,但这并不能解决问题。
模块中的相关代码
MODULE nrtype
INTEGER, PARAMETER :: I4B = SELECTED_INT_KIND(9)
INTEGER, PARAMETER :: SP = KIND(1.0)
END MODULE nrtype
MODULE nr
INTERFACE
SUBROUTINE test(x,y)
USE nrtype
REAL(SP), INTENT(IN) :: x
REAL(SP), INTENT(OUT) :: y
END SUBROUTINE test
END INTERFACE
INTERFACE
SUBROUTINE flmoon(n,nph,jd,frac)
USE nrtype
INTEGER(I4B), INTENT(IN) :: n,nph
INTEGER(I4B), INTENT(OUT) :: jd
REAL(SP), INTENT(OUT) :: frac
END SUBROUTINE flmoon
END INTERFACE
INTERFACE bessj
FUNCTION bessj_s(n,x)
USE nrtype
INTEGER(I4B), INTENT(IN) :: n
REAL(SP), INTENT(IN) :: x
REAL(SP) :: bessj_s
END FUNCTION bessj_s
FUNCTION bessj_v(n,x)
USE nrtype
INTEGER(I4B), INTENT(IN) :: n
REAL(SP), DIMENSION(:), INTENT(IN) :: x
REAL(SP), DIMENSION(size(x)) :: bessj_v
END FUNCTION bessj_v
END INTERFACE
END MODULE nr
更新:根据Vladimir F的回答,我创建了 test.f90,
function test(x, y)
real, intent(in) :: x
real, intent(out) :: y
y = x + 2
return
end function test
real(SP) :: z = 1, t
在 helloTest 中更新,编译为
gfortran -o test test.f90 nr.o nrtype.o helloTest.f90
结果得到了我的 3.0000。谢谢!