2016-12-16 73 views
0

尽管我列入“#包括”我的代码,当我使用内置qsort函数,铛给我的错误:铛链接错误:未定义参考“快速排序”

schedule.o: In function `chooseTicket': 
schedule.c:(.text+0x16d): undefined reference to `qsort' 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

启动该文件(schedule.c)是这样的:

#include "sched.h" 
#include "schedproc.h" 
#include <assert.h> 
#include <minix/com.h> 
#include <machine/archtypes.h> 
#include <stdlib.h> 
#include <lib.h> 
#include <string.h> 
#include <time.h> 

这里是我使用的快速排序的内置函数的函数

int chooseTicket(int* ticketList,int length,int totalTicket){ 
     int randomValue; 
     int temp=0,prevTemp=0,selectedTicket=0,selectedIndex = 0; 
     time_t t; 
     struct schedproc *rmp; 
     int* sortedTicketList = malloc(length*sizeof(int)); 
     memcpy(sortedTicketList,ticketList,length); 
     srandom((unsigned)time(&t)); 
     randomValue = (random() % totalTicket); 
     qsort(sortedTicketList,length,sizeof(int),cmpFunc);//this line 

注意:同样的错误也发生在'rand()'和'srand()'函数,而我使用'random()'和'srandom()',那么问题就解决了。尽管'rand()'和'srand()'是普遍接受的函数,头文件包含这些函数,但我不明白为什么铿锵声给出了链接错误,而我正在使用'rand()'和'srand )。

+1

旁白:。移动'srandom((无符号)时间(&t));'到'main()的'你应该播种RNG的[只有一次 –

+1

可能的复制什么是未定义参考/解析的外部符号错误,我怎么修复它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-doi-i-fix) –

+0

Can你展示了你用来构建你的软件的clang命令? – chrisaycock

回答

0

首先,qsort不是built-in,但C standard library的部分(正式名称为托管环境。)

其次,你需要学习的是#include只允许访问的功能的声明在任何给定库。您需要链接库,以便您的程序实际执行对函数的调用。由于您在这里遇到了链接器错误,因此没有#include可以提供帮助。

我想你正在写一个MINIX服务,因此链接到libminc而不是完整的标准库(“libc”);换句话说,这是一个独立的环境。和它发生qsort()没有在有限的一组C函数included in libminc.

要么与qsort.(c|o)具体链路;或者将您自己的libminc本地版本扩展为包含qsort();或吃整个蛋糕和链接充分libc,也许通过添加DPADD+= ${LIBC}; LDADD+= -lcMakefile(我从来没有试过这样做,但它应该在某个时间点工作,according to the code;这是不正常的做法,所以期待问题的道路上。)

相关问题