作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 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/
我是 fortran 和 Linux 的初学者。我在 Linux 上运行了一个名为 demo.f90 的简单 fortran 程序。然后出现错误,如下。 /tmp/cckAhxOW.o: In fun
我是一名优秀的程序员,十分优秀!