Despite the fact that i included '#include ' to my code, when i use built-in qsort function, clang gives me the error:
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)
start of the file (schedule.c) is like that:
#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>
and here is the function in which i used qsort built-in function
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
note: Same errors also occured for 'rand()' and 'srand()' function and instead i have used 'random()' and 'srandom()', then the problem was solved. I don't understand despite the fact that 'rand()' and 'srand()' is generally accepted functions and header file contains these functions, why clang gives me linking errors while i am using 'rand()' and 'srand().
First,
qsort
is not a built-in, but part of the C standard library (formally, for hosted environments.)Second, you need to learn that
#include
only allows access to the declarations of the functions in any given library. You need to link with the library, for your program to actually perform the call to the functionnality. Since you are getting a linker error here, no#include
are going to help.I guess you are writing a MINIX service, hence linking with
libminc
rather than with the full standard library ("libc
"); in other words, this is a freestanding environment. And it happensqsort()
is not in the restricted set of C functions included inlibminc
.Either link with
qsort.(c|o)
specifically; or expand your own local version oflibminc
to includeqsort()
; or eat the whole cake and link with fulllibc
, perhaps by addingDPADD+= ${LIBC}; LDADD+= -lc
to theMakefile
(I never tried to do that but it was supposed to work at some point, according to the code; it is not usual practice, so expect problems down the road.)