对相同模块中定义的过程未定义的引用 [英] Undefined reference to procedure defined in the same module

查看:147
本文介绍了对相同模块中定义的过程未定义的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试熟悉Fortran中的模块,并且已经使用模块创建了一个简单的代码。源代码已附加。

主程序

 程序main_prog 
使用mod_f_g_h

隐式无
! f,g,h,s在mod_f_g_h
real :: x,y

!阅读x,y
print *,输入x和y
阅读*,x,y

!检查
if(y == 0)然后
print *,y must be non-zero
stop
end if

!在屏幕上打印
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)

结束程序main_prog

MODULE

  module mod_f_g_h 

!使用其他模块
隐式无

包含

实函数s(x)
隐函数
real :: x,g
s = g(x,x)
结束函数s

实函数f(x,y)
隐式无
实型:: x,y
f = x + y
结束函数f

实函数g(x,y)
隐式无
real :: x,y
g = x * y
结束函数g

实函数h(x,y)
隐式无
real :: x,y
h = x / y
结束函数h
结束模块mod_f_g_h

但是当我尝试编译并链接代码时,使用

  gfortran -c mod_f_g_h.f90 
gfortran -c main.f90
gfortran * .o -o run.x

出现以下错误(在最后一步)

  mod_f_g_h.o:在函数`__mod_f_g_h_MOD_s'中:
mod_f_g_h.f90 :(。text + 0xb6):未定义的引用`g_'
collect2:ld返回1退出状态

我想,我定义了所有的变量,而且我没有链接任何外部库,不知道为什么会出现这个错误?任何评论/想法?

解决方案

它应该是

结束函数s 

模块内部。



VladimirF进一步解释:


通过 real g 你在 s 中声明了外部函数的函数 g em>实际功能与模块中的不同。



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.

MAIN PROGRAM

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

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?

解决方案

It should read

real function s(x)
implicit none
real :: x
s = g(x,x)
end function s

inside the module.

Further explanation by VladimirF:

By real g you were declaring the function g inside s as an external real function distinct from that one in the module.

这篇关于对相同模块中定义的过程未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆