在OS X 10.8上使用gfortran非法重新定位函数指针

时间:2014-01-19 17:38:27

标签: macos fortran shared-libraries gfortran fortran-iso-c-binding

我在OS X 10.8系统上收到以下错误消息,其中Xcode 5.0.2和gfortran 4.8.2通过自制软件安装。

% gfortran -shared f03_iso_c.f03
ld: illegal text-relocation to '_compute' in /var/folders/w3/_r4gjs1d363176f99wgkpwl80000gn/T//cc8VFCuI.o from 'anon' in /var/folders/w3/_r4gjs1d363176f99wgkpwl80000gn/T//cc8VFCuI.o for architecture x86_64
collect2: error: ld returned 1 exit status

f03_iso_c.f03的内容是

module F03_ISO_C
  use, intrinsic :: iso_c_binding

  contains

  integer(c_int) function compute() bind(c) result(compute)
    use, intrinsic :: iso_c_binding
    implicit none

    compute = 1
    return

  end function compute

end module F03_ISO_C

integer function F03_ISO_C_init()
  use F03_ISO_C
  use, intrinsic :: iso_c_binding

  implicit none

  call USE_FUNC_PTR(c_funloc(compute))

  F03_ISO_C_init = 1
  return

end function F03_ISO_C_init

subroutine USE_FUNC_PTR(f1)
  use, intrinsic :: iso_c_binding

  implicit none

  type(c_funptr) :: f1

  print *, f1, "is the address"

end subroutine USE_FUNC_PTR

非常感谢您解决此错误的任何帮助。

1 个答案:

答案 0 :(得分:0)

我会说函数compute()编写得不好,试试这个:

function compute() bind(c) result(out)
    use, intrinsic :: iso_c_binding
    implicit none
    integer(c_int) :: out

    out = 1
    return

 end function compute