在Fortran中排序,对qsort_的未定义引用 [英] Sorting in Fortran, undefined reference to qsort_
问题描述
我是Fortran和Linux的初学者。我在Linux上运行了一个名为 demo.f90
的简单Fortran程序。然后发生错误,如下所示。
$ b $ pre $
/tmp/cckAhxOW.o:函数MAIN__:
demo.f90 :( .text + 0x25 ):对'qsort_'的未定义引用
代码如下。
程式转换
外部比较
整数* 2比较
INTEGER * 4 array(10)/ 5,1,9,0,8,7,3,4,6,2 /,l / 10 /,isize / 4 /
调用qsort(数组, l,isize,compar)
write(*,'(10i3)')array
end program trand
integer * 2 function compar( a,b)
INTEGER * 4 a,b
如果(a .lt。b)比较= -1
如果(a。 b)compar = 0
如果(a .gt。b)compar = 1
返回
结束函数比较
您最好把比较 code>放入模块,
使用
它。这种方法将允许编译器检查你的子程序调用和它的声明之间的一致性。如果主程序和模块在同一个文件中,则首先放置模块。
module MySubs
包含
整数* 2函数比较(a ,b)
...
结束函数比较
结束模块MySubs
程序转换
使用MySubs
使用SomeMod
....
结束程式转换
其中 SomeMod
是另一个具有排序例程qsort的文件SomeMod.f90中的模块。然后编译并链接:
gfortran SomeMod.f90 trand.f90
或者您正在使用的任何编译器和文件名。包含 qsort
的文件需要位于具有程序交易
的文件之前。
I'm a beginner of fortran and Linux.I ran a simple fortran program named demo.f90
on Linux. Then an error occurred, as follow.
/tmp/cckAhxOW.o: In function `MAIN__':
demo.f90:(.text+0x25): undefined reference to `qsort_'
The code is attached below.
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
You would do better to put compare
into a module and use
it. This approach will allow the compiler to check for consistency between your call of the subroutine and its declaration. If the main program and the module are in the same file, put the module first.
module MySubs
contains
integer*2 function compar( a, b )
...
end function compar
end module MySubs
program trand
use MySubs
use SomeMod
....
end program trand
Where SomeMod
is a module in another file SomeMod.f90 with the sorting routine qsort. Then compile and link:
gfortran SomeMod.f90 trand.f90
or whatever compiler and filenames you are using. The file with qsort
needs to be before the file with program trand
.
这篇关于在Fortran中排序,对qsort_的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!