我试图将某些Fortran子例程表示为c_funptr
(void *
),以通过漂亮的fdict库创建字典。在此之后,我尝试致电GCC文档c_funloc
。但是gfortran似乎返回一个c_funptr
数组而不是标量值。
这是编译器中的错误,还是我缺少重要的东西?
来自的输出gfortran -v
:
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp --enable-cet=auto
Thread model: posix
gcc version 8.3.0 (GCC)
我还尝试了使用ifort(19.0.2.187版),它给出了所需的行为(请参见下文)。
MWE:
! = minimum.f90 =
module test
use iso_c_binding
implicit none
interface test_funptr
module procedure test_funptr0
module procedure test_funptr1
end interface test_funptr
contains
subroutine test_funptr0(fp)
type(c_funptr) :: fp
write(*,*) "fp0!"
end subroutine test_funptr0
subroutine test_funptr1(fp)
type(c_funptr), dimension(:) :: fp
write(*,*) "fp1!", shape(fp)
end subroutine test_funptr1
function bar(x) result(y) bind(c)
real(c_double) :: x
real(c_double) :: y
y = -x**2 + x + 1
end function bar
end module test
program main
use iso_c_binding
use test
implicit none
call test_funptr(c_funloc(bar))
end program main
用...编译 gfortran minimum.f90 -o min
各地预期产量:
fp0
基本行为:fp1
对于gfortran,fp0
对于Intel编译器,形状为零。
也许我只是缺少gfortran的正确选择?
c_funloc
内部模块的功能iso_c_binding
由Fortran标准指定,其功能结果为标量类型c_funptr
(Fortran 2018、18.2.3.5,在Fortran 2003和Fortran 2008中类似)。
当程序本身符合要求时,使函数结果为数组将违反Fortran标准。
但是,在这种情况下,gfortran无法提供数组函数结果:您可以使用类似以下的简单方法进行测试
print*, SHAPE(C_FUNLOC(bar))
相反,gfortran无法将通用过程正确解析test_funptr
为特定的test_funptr0
。
也考虑这种情况
use, intrinsic :: iso_c_binding, only : c_funptr, c_null_funptr
use test, only : test_funptr, test_funptr1
type(c_funptr) :: ptr = C_NULL_FUNPTR
call test_funptr(ptr)
call test_funptr1(ptr)
end
gfortran 8错误地解析了泛型,并允许直接调用test_funptr1
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句