对同一模块中定义的过程的未定义引用 [英] Undefined reference to procedure defined in the same module
问题描述
我正在尝试熟悉 fortran 中的模块,为此我创建了一个带有模块的简单代码.附上源代码.
I am trying to get familiarize with modules in fortran and for that I have created a simple code with module. The source code is attached.
主程序
program main_prog
use mod_f_g_h
!
implicit none
! f,g,h,s are defined in mod_f_g_h
real :: x,y
!
! read x,y
print *, "enter x and y "
read*, x, y
!
! check
if( y == 0 ) then
print*, "y must be non-zero"
stop
end if
!
! print on screen
print*, ' x = ', x
print*, ' y = ', y
print*, 'x + y = ', f(x,y)
print*, 'x * y = ', g(x,y)
print*, 'x * x = ', s(x)
print*, 'y * y = ', s(y)
print*, 'x / y = ', h(x,y)
end program main_prog
模块
module mod_f_g_h
!
! use other modules
implicit none
!
contains
!
real function s(x)
implicit none
real :: x,g
s = g(x,x)
end function s
!
real function f(x,y)
implicit none
real :: x,y
f = x+y
end function f
!
real function g(x,y)
implicit none
real :: x,y
g = x*y
end function g
!
real function h(x,y)
implicit none
real :: x,y
h = x/y
end function h
end module mod_f_g_h
但是当我尝试编译和链接代码时,使用
But when I try to compile and link the code, using
gfortran -c mod_f_g_h.f90
gfortran -c main.f90
gfortran *.o -o run.x
出现以下错误(在最后一步)
Got the following error (in last step)
mod_f_g_h.o: In function `__mod_f_g_h_MOD_s':
mod_f_g_h.f90:(.text+0xb6): undefined reference to `g_'
collect2: ld returned 1 exit status
我想,我已经定义了所有的变量,而且我没有链接任何外部库,所以我不知道为什么会出现这个错误?有什么意见/想法吗?
I think, I have defined all the variables, moreover I am not linking any external library, so I don't know why this error is appearing? Any comment/idea?
推荐答案
应该阅读
real function s(x)
implicit none
real :: x
s = g(x,x)
end function s
模块内部.
VladimirF 的进一步解释:
Further explanation by VladimirF:
通过real g
,您将s
内的g
函数声明为一个external 真实函数,与此不同模块中的一个.
By
real g
you were declaring the functiong
insides
as an external real function distinct from that one in the module.
这篇关于对同一模块中定义的过程的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!