You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HP-UX下HP Fortran编译数组重定义程序符号缺失问题求助

问题描述

在HP-UX rx2800 B.11.31系统上,使用HP Fortran 90编译器编译一段修改数组维度的示例程序时,出现链接错误。

示例程序代码:

program arrayresize
  implicit none
  integer, allocatable :: array1(:)
  integer, pointer :: array2(:,:)

  ! allocate and initialize array1
  allocate(array1(6))
  array1 = (/1,2,3,4,5,6/)

  ! This starts out initialized to 2
  print *, 'array1(2) = ', array1(2)

  ! Point array2 to same data as array1. The shape of array2
  ! is passed in as an array of intergers because C_F_POINTER
  ! uses and array of intergers as a SIZE parameter.
  array2 => getArray(array1, (/2,3/))

  ! Change the value at array2(2,1) (same as array1(2))
  array2(2,1) = 5

  ! Show that data in array1(2) was modified by changing
  ! array2(2,1)
  print *, 'array(2,1) = array1(2) = ', array1(2)

contains

  function getArray(array, shape_) result(aptr)
    use iso_c_binding, only: C_LOC, C_F_POINTER
    ! Pass in the array as an array of fixed size so that there
    ! is no array descriptor associated with it. This means we
    ! can get a pointer to the location of the data using C_LOC
    integer, target :: array(1)
    integer :: shape_(:)
    integer, pointer :: aptr(:,:)

    ! Use C_LOC to get the start location of the array data, and
    ! use C_F_POINTER to turn this into a fortran pointer (aptr).
    ! Note that we need to specify the shape of the pointer using an
    ! integer array.
    call C_F_POINTER(C_LOC(array), aptr, shape_)
  end function
end program

执行编译命令:

f90 +DD64 -o ./h.out h.f

出现的链接错误:

47 Lines Compiled
ld: Unsatisfied symbol "iso_c_binding$c_loc_array_1d_int4" in file h.o
ld: Unsatisfied symbol "iso_c_binding$c_f_pointer_array_2d_4s_int4" in file h.o
2 errors.

询问是否需要加载特定库或其他解决办法。

解决方法
  • 链接时添加ISO C绑定库:HP Fortran 90编译器使用iso_c_binding模块时,需显式链接对应库文件。在编译命令中加入-lcaf参数(C Array Facility库),修改后的编译命令为:

    f90 +DD64 -o ./h.out h.f -lcaf
    

    该库包含iso_c_binding模块相关的符号实现,可直接解决缺失问题。

  • 检查编译器版本兼容性:若上述方法无效,确认HP Fortran 90编译器版本是否完整支持iso_c_binding模块特性。部分旧版本对Fortran 2003标准的iso_c_binding支持不完善,可尝试升级编译器至较新版本。

  • 替代实现方案:若无法链接库或升级编译器,可改用HP Fortran特有的数组重映射方式,比如结合RESHAPE函数与指针操作,但这种方式会生成数组副本,无法共享原数组内存空间。如果需要内存共享,优先使用前两种方法。

内容的提问来源于stack exchange,提问作者László Battha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 15:20:26