linux - 在 Fortran 中排序,未定义对 qsort_ 的引用

标签 linux fortran

我是 fortran 和 Linux 的初学者。我在 Linux 上运行了一个名为 demo.f90 的简单 fortran 程序。然后出现错误,如下。

/tmp/cckAhxOW.o: In function `MAIN__':
demo.f90:(.text+0x25): undefined reference to `qsort_'

代码附在下面。

program trand

external compar

integer*2 compar

INTEGER*4 array(10)/5,1,9,0,8,7,3,4,6,2/,l/10/,isize/4/

call qsort( array, l, isize, compar )

write(*,'(10i3)') array

end program trand

integer*2 function compar( a, b )

INTEGER*4 a, b

if ( a .lt. b ) compar = -1

if ( a .eq. b ) compar = 0

if ( a .gt. b ) compar = 1

return

end function compar

最佳答案

qsort 实际上是一个 C 标准库函数。您必须为其声明一个接口(interface)。它是这样的:

module Sort
  use iso_c_binding

  implicit none

  interface
    subroutine qsort(array,elem_count,elem_size,compare) bind(C,name="qsort")
      import
      type(c_ptr),value       :: array
      integer(c_size_t),value :: elem_count
      integer(c_size_t),value :: elem_size
      type(c_funptr),value    :: compare !int(*compare)(const void *, const void *)
    end subroutine qsort !standard C library qsort
  end interface
end module Sort


program trand

use Sort

external compar

integer(c_int) compar

integer(c_int),target :: array(10) = [5,1,9,0,8,7,3,4,6,2]
integer(c_size_t) l/10/,isize/4/

call qsort( c_loc(array(1)), l, isize, c_funloc(compar) )

write(*,'(10i3)') array

end program trand

integer(c_int) function compar( a, b ) bind(C)
use iso_c_binding

integer(c_int) a, b

if ( a .lt. b ) compar = -1

if ( a .eq. b ) compar = 0

if ( a .gt. b ) compar = 1

end function compar

然后像你做的那样编译。使用 gfortran 4.8 测试成功。

关于linux - 在 Fortran 中排序,未定义对 qsort_ 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20941575/

相关文章:

python - linux下通过脚本/命令行替换PE EXE文件中的资源

linux - 用于复制文件和文件夹并执行命令的简单 shell 脚本

c - 什么是 "column independent code"?

parallel-processing - 内部 dot_product 比 a*a+b*b+c*c 慢?

c - csc 中 spmv 的 openmp 并行化

php - PHP中的通配符复制文件

c - 在同一 linux 线程中运行 select() 套接字和定时器

Linux 看门狗和 NTP

time - 为 Fortran 多线程程序计时

fortran - 如何在 Fortran 中获取派生类型组件的编号、名称和值