gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:34:58 27 4
gpt4 key购买 nike

我是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com