【发布时间】:2019-12-28 06:19:24
【问题描述】:
当变量是COMMON 块的一部分时,我无法从 Fortran EXE 正确访问 Fortran DLL 中的变量。
我有一个简单的代码 simple.f90,我使用 MSYS64/MinGW-w64 gfortran 9.2 as 将其编译成 DLL
x86_64-w64-mingw32-gfortran simple.f90 -o simple.dll -shared
! simple.f90
module m
implicit none
integer :: a, b
!common /numbers/ a, b
end module
subroutine init_vals
use m
implicit none
a = 1
b = 2
end subroutine
这个库是从一个更简单的程序prog.f90中使用的,编译为
x86_64-w64-mingw32-gfortran prog.f90 -o prog -L. -lsimple
! prog.90
program p
use m
implicit none
print *, 'Before', a, b
call init_vals
print *, 'After', a, b
end program
当 COMMON 块 /numbers/ 被注释掉时,代码工作并打印预期结果:
Before 0 0
After 1 2
但是,当我取消注释 COMMON 块时,输出变为
Before 0 0
After 0 0
好像程序使用的变量突然与库中使用的变量不同。
这两种变体在带有 gfortran 9.1 的基于 Linux 的操作系统中同样有效。
我知道“在某些系统上,过程和全局变量(模块变量和 COMMON 块)在共享库中时需要特殊处理才能访问”,如下所述:https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gfortran/GNU-Fortran-Compiler-Directives.html。但是,我无法插入该类型的语句
!GCC$ ATTRIBUTES DLLIMPORT :: numbers
或
!GCC$ ATTRIBUTES DLLEXPORT :: numbers
代码中的任何位置,而不会被编译器捕捉到。
【问题讨论】:
-
尝试在 ATTRIBUTES 指令中使用
/numbers/。这就是在 ifort 中的做法。 -
代码是无效的Fortran,所以gfortran可以为所欲为。
-
@SteveLionel 感谢您的建议,但是当我将
numbers括在斜杠中时,gfortran 会吐出“错误:名称中的字符无效”。 @evets 您能否更具体地说明代码的哪一部分无效以及如何解决? -
当您尝试使用
print *, "Before", a, b打印它们时,您期望a和b具有什么价值?a和b未定义!如果您使用模块,请不要使用common。 -
这是 gfortran 中的一个已知错误 gcc.gnu.org/bugzilla/show_bug.cgi?id=47030
标签: dll fortran gfortran mingw-w64