开发者

Accessing a Fortran module in a function defined in some other file

开发者 https://www.devze.com 2022-12-31 01:40 出处:网络
I am using Fortran 90. I have defined a Fortran module in fileA.f as: module getArr double precision a(100)

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?开发者_C百科 How should I declare my function?


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.


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 a use 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消