链接fortran模块:“未定义参考” [英] Linking fortran module: "undefined reference"

查看:386
本文介绍了链接fortran模块:“未定义参考”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在模块中编写一些函数/子例程,这些模块调用同一模块中的另一个函数并运行链接器错误。显示相同行为的玩具示例:

 !in test.f 

module m1
隐式无
包含
实函数mult(a,b)
real :: a
real :: b
mult = a * b
return
结束函数mult

实数函数sq(a)
real :: a,mult
sq = mult(a,a)
return
结束函数sq

结束模块m1

程序主体
使用m1
写入(*,*)sq(2.0)
end program

当我尝试编译这个时,遇到了麻烦:

  [christopher @ archlinux metropolis] $ gfortran -ffree-form test.f 
/tmp/ccpzdTLE.o:在函数`__m1_MOD_sq'中:
test.f :(。text + 0x20):对'mult_'的未定义引用
collect2:错误:ld返回1退出状态

另一方面,仅编译( gfortran -c -ffree-form test.f -Wall )运行时没有投诉。

现在这个厕所ks适合所有世界,就像一个编译器错误---在模块中,当它真的应该与合并时,它会引用 mult_ __m1_MOD_sq ---但我很难相信这是一个编译器错误,而不是我做一些愚蠢的事情。



DDG didn'发现任何有用的东西。在将模块从一个主文件中分离出来时,大多数类似的问题都会发生。在这些情况下,当模块与程序在同一个文件中时,这种情况是有效的,这里不是这种情况。我查看了Fortran模块中的一些页面,并没有看到任何相关的信息。



任何人都可以指出适当的文档,或者更好地解释发生了什么以及如何修复它?

解决方案

您不需要在函数sq中声明函数mult,即没有需要real :: mult。 sq已经知道了mult,因为它在同一个模块中。 mult的接口以sq为已知,因为它们在同一个模块中。 mult和sq的接口在主程序中是已知的,因为它使用了模块。让模块提供接口和声明让编译器感到困惑。


I'm trying to write some functions/subroutines in a module that call another function in the same module and running into linker errors. A toy example displaying the same behavior:

!in test.f

module m1
  implicit none
contains
  real function mult(a, b)
    real :: a
    real :: b
    mult = a * b
    return
  end function mult

  real function sq(a)
    real :: a, mult
    sq = mult(a, a)
    return
  end function sq

end module m1

program main
use m1
write(*,*) sq(2.0)
end program

When I try to compile this, I run into trouble:

[christopher@archlinux metropolis]$ gfortran -ffree-form test.f
/tmp/ccpzdTLE.o: In function `__m1_MOD_sq':
test.f:(.text+0x20): undefined reference to `mult_'
collect2: error: ld returned 1 exit status

On the other hand, compiling only (gfortran -c -ffree-form test.f -Wall) runs with no complaint.

Now this looks for all the world like a compiler error---in the module it comes up with a reference to mult_ when it really ought to com up with __m1_MOD_sq---but I have a very hard time believing that this is a compiler bug rather than me doing something stupid.

DDG didn't turn up anything useful. Most of the similar problems ocurred in splitting the module off from one main file. In those cases, things worked when the module was in the same file as the program, which is not the case here. I looked at a number of pages on modules in Fortran and didn't see anything relevant.

Can anyone point me to appropriate documentation or, better yet, explain what's going on and how I can fix it?

解决方案

You don't need to declare function mult in function sq, i.e., there is no need for "real :: mult". sq already "knows" about mult since it is in the same module. The interface of mult is known to sq since they are in the same module. The interface of mult and sq are known to the main program since it uses the module. Having both the module providing the interface and the declaration is confusing the compiler.

这篇关于链接fortran模块:“未定义参考”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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