2016-12-16 76 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;這是不正常的做法,所以期待問題的道路上。)