0

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().

ffguven
  • 187
  • 11
  • 1
    Aside: move `srandom((unsigned)time(&t));` to `main()`. You should seed the RNG once only. – Weather Vane Dec 16 '16 at 20:25
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Dec 16 '16 at 20:26
  • Can you show the clang command you are using to build your software? – chrisaycock Dec 16 '16 at 20:26
  • How did you compile and link your code? – e0k Dec 16 '16 at 20:26
  • because i am working on minix operating system and main() function is in kernel, integrating this functionality to kernel's main.c is so complicated. i need to do it in this function. – ffguven Dec 16 '16 at 20:26
  • i am just using 'make hdboot' in '/usr/src/releasetools' path in minix. I am sorry about not saying this minix detail. i'm gonna add it to the question. but i think there is a problem with clang, not minix. – ffguven Dec 16 '16 at 20:27
  • 1
    Moving more off-topic sorry: move the `srandom()` call to whatever you have as an `init` function. The worst thing is to seed the RNG every time you want a random number. – Weather Vane Dec 16 '16 at 20:28
  • i am not sure about is it possible to seed 'srand()' in any part of huge code and then using 'rand()' without seeding 'srand()' again everywhere. because approximately 20.000 lines of code is working on kernel – ffguven Dec 16 '16 at 20:31
  • 2
    OP: You have posted a [minix related question](http://stackoverflow.com/questions/41190568/linking-error-in-minix-system-call-that-changes-sched-process-state) Stop using SO as a means to help you in debugging, this is not what SO is about. – t0mm13b Dec 16 '16 at 20:33
  • Then put `static int seeded = 0;` in the `random()` function and make the seeding dependant on that. – Weather Vane Dec 16 '16 at 20:34
  • 1
    A kernel, whether for Linux, MInix, BSD, Solaris, AIX, Windows or any other system, does not necessarily supply all the functions that are provided in a 'hosted implementation' (such as provided outside the kernel on all those systems). You have to know which functions are provided by your kernel. You have to avoid using functions which are not provided, or arrange for a (private?) implementation of the function to be available. – Jonathan Leffler Dec 16 '16 at 20:44
  • Kernel environments normally don't use the full standard C library. They are [freestanding](http://stackoverflow.com/questions/30825151/is-there-a-meaningful-distinction-between-freestanding-and-hosted-implementation) implementations. I wonder how you manage to have `malloc`. – n. m. could be an AI Dec 16 '16 at 20:46
  • thank you for your valuable comment Mr. Leffler. i will think about it. – ffguven Dec 16 '16 at 20:46

1 Answers1

1

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 happens qsort() is not in the restricted set of C functions included in libminc.

Either link with qsort.(c|o) specifically; or expand your own local version of libminc to include qsort(); or eat the whole cake and link with full libc, perhaps by adding DPADD+= ${LIBC}; LDADD+= -lc to the Makefile (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.)

AntoineL
  • 888
  • 4
  • 25