Lora-net/packet_forwarder

Build Error on Alpine linux: undefined reference to `qsort_r'

mojtaba-esk opened this issue · 2 comments

Hi I just wanna compile the forwarder to use it in an Alpine based container, I faced this error on building:

src/jitqueue.c: In function 'jit_sort_queue':
src/jitqueue.c:118:5: warning: implicit declaration of function 'qsort_r'; did you mean 'qsort'? [-Wimplicit-function-declaration]
  118 |     qsort_r(queue->nodes, queue->num_pkt, sizeof(queue->nodes[0]), compare, &counter);
      |     ^~~~~~~
      |     qsort
gcc -c -O2 -Wall -Wextra -std=c99 -Iinc -I. -I../../lora_gateway/libloragw/inc src/timersync.c -o obj/timersync.o
gcc -L../../lora_gateway/libloragw obj/lora_pkt_fwd.o obj/parson.o obj/base64.o obj/jitqueue.o obj/timersync.o -o lora_pkt_fwd -lloragw -lrt -lpthread -lm
/usr/lib/gcc/armv7-alpine-linux-musleabihf/9.3.0/../../../../armv7-alpine-linux-musleabihf/bin/ld: obj/jitqueue.o: in function `jit_sort_queue':
jitqueue.c:(.text+0xd2): undefined reference to `qsort_r'
collect2: error: ld returned 1 exit status
make: *** [Makefile:65: lora_pkt_fwd] Error 1

it seems like function qsort_r is removed from mini libc that is used in Alpine.

qsort_r() is indeed often missing from smaller libc's, so you need to work around this by using qsort() instead - the main difference being that qsort_r() can pass a custom argument to the compare function, and plain qsort() cannot.

A crude but effective solution for the limited way this is actually used in the jitqueue is to create a global variable as a comparable pointer type which you can set to the desired value before invoking qsort() and then access in the compare function passed to qsort(). As long as you don't have concurrent use of that compare function (the jitqueue stuff sits inside its its own mutex so this would come down to not re-using it elsewhere) it should be fine -

Thank you for your inquiry.

Customers are encouraged to submit technical questions via our dedicated support portal at https://semtech.force.com/ldp/ldp_support.

We invite all users to visit the LoRa Developer Portal Forum at https://forum.lora-developers.semtech.com and to join the thriving LoRa development community!