访问其他文件中定义的函数中的 Fortran 模块
我使用的是 Fortran 90。我在 fileA.f
中定义了一个 Fortran 模块:
module getArr
double precision a(100)
end module getArr
相同的 fileA.f
包含一个使用此模块的子例程:
subroutine my_sub
use getArr
implicit none
a(1) = 10.5
end subroutine
在 fileB 中.f
,我有一个 Fortran 函数。我试图访问 a(1)
的值:
double precision function my_func(R)
use getArr
double precision x
x = a(1)
return
end
但我在编译时遇到错误。它表示无法访问模块getArr
。这是否与在函数中使用模块而不是在子例程中使用模块有关?我应该如何声明我的函数?
I am using Fortran 90. I have defined a Fortran module in fileA.f
as:
module getArr
double precision a(100)
end module getArr
The same fileA.f
contains a subroutine that uses this module:
subroutine my_sub
use getArr
implicit none
a(1) = 10.5
end subroutine
In fileB.f
, I have a Fortran function. I am trying to access the value of a(1)
as:
double precision function my_func(R)
use getArr
double precision x
x = a(1)
return
end
But I am getting errors at the compile time. It says it is unable to access the module getArr
. Is this something to do with the use of a module within a function as opposed to within a subroutine? How should I declare my function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TED 关于语法的说法是正确的——“getArr%”不是数组“a”名称的一部分。该表示法用于用户派生类型。
语言标准之外的另一个方面——编译源代码:
对于大多数编译器,您需要按顺序编译文件,将包含模块的源代码文件放在使用该模块的任何单独文件之前。编译器必须“了解”模块才能使用它。
另外,你的例子中有主程序吗?
如果仍然无法正常工作,请向我们显示确切的错误消息。
T.E.D. is correct about the syntax -- "getArr%" is not part of the name of the array "a". That notation is used for a user-derived type.
Another aspect that is outside the language standard -- compiling the source code:
With most compilers, you need to compile your files in order, placing the source-code file that contains a module before any separate file that uses it. The compiler has to "know" about a module before it can use it.
Also, do you have a main program in your example?
If it still doesn't work, please show us the exact error message.
看起来您正在尝试使用
getArr%
作为某种模块说明符。你确定这是对的吗?我不是 f90 专家,但我的编译器似乎不支持类似的东西。一旦您执行use
,该模块中的所有内容都可以在本地使用,就像您在子例程中声明的那样。尝试删除该
getArr%
并看看会发生什么。It looks like you are trying to use
getArr%
as some kind of module specifier. Are you sure that's right? I'm not an f90 expert, but my compiler doesn't seem to support anything like that. Once you do ause
all the stuff in that module is available locally just like you declared it in your subroutine.Try removing that
getArr%
and see what happens.