Saturday, March 30, 2013

Compiling libhid for Raspbian Linux on a Raspberry Pi


My son and I are working on a project using a Raspberry Pi and I needed to be able to talk to a USB HID device. This requires a software library called libhid but unfortunately it is not available as a package on Raspbian linux. I downloaded the source and attempted to compile it but ran into an error:

lshid.c:32:87: error: parameter ‘len’ set but not used [-Werror=unused-but-set-parameter]
cc1: all warnings being treated as errors
make[2]: *** [lshid.o] Error 1
make[2]: Leaving directory `/root/libhid-0.2.16/test'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/root/libhid-0.2.16'
make: *** [all] Error 2

After some googling I found a couple others on the Raspberry Pi forums that ran into the same problem. One of the commenters came up with a simple fix that requires a quick edit of the source code. In ~/libhid-0.2.16/test you need to edit the file lshid.c

Here is the code before making the edit:

39 /* only here to prevent the unused warning */
40 /* TODO remove */
41 len = *((unsigned long*)custom);
42
43 /* Obtain the device's full path */


Here is the code after the edit.
You need to comment out line 41 and then add len = len; and custom = custom;

39 /* only here to prevent the unused warning */
40 /* TODO remove */
41 //len = *((unsigned long*)custom);
42 len = len;
43 custom = custom;
44
45 /* Obtain the device's full path */

After editing the file simply run configure, make and make install like normal. The library will be put into /usr/local. Make sure you run sudo ldconfig before trying to compile any software that uses libhid. Thanks Raspberry Pi forums!

5 comments:

  1. Hello, I compiled libhid but run error:

    make all-recursive
    make[1]: Entering directory `/usr/local/lib/libhid-0.2.16'
    Making all in hidparser
    make[2]: Entering directory `/usr/local/lib/libhid-0.2.16/hidparser'
    make[2]: Nothing to be done for `all'.
    make[2]: Leaving directory `/usr/local/lib/libhid-0.2.16/hidparser'
    Making all in src
    make[2]: Entering directory `/usr/local/lib/libhid-0.2.16/src'
    if /bin/bash ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I. -I.. -DNDEBUG -fPIC -I../include -I../hidparser -O2 -Wall -W -Werror -MT linux.lo -MD -MP -MF ".deps/linux.Tpo" -c -o linux.lo linux.c; \
    then mv -f ".deps/linux.Tpo" ".deps/linux.Plo"; else rm -f ".deps/linux.Tpo"; exit 1; fi
    gcc -DHAVE_CONFIG_H -I. -I. -I.. -DNDEBUG -fPIC -I../include -I../hidparser -O2 -Wall -W -Werror -MT linux.lo -MD -MP -MF .deps/linux.Tpo -c linux.c -fPIC -DPIC -o .libs/linux.o
    In file included from linux.c:6:0:
    ../include/hid.h:5:17: fatal error: usb.h: No such file or directory
    compilation terminated.
    make[2]: *** [linux.lo] Error 1
    make[2]: Leaving directory `/usr/local/lib/libhid-0.2.16/src'
    make[1]: *** [all-recursive] Error 1
    make[1]: Leaving directory `/usr/local/lib/libhid-0.2.16'
    make: *** [all] Error 2
    pi@raspberrypi /usr/local/lib/libhid-0.2.16


    Could you help me please?

    ReplyDelete
    Replies
    1. The fatal error was caused by a missing library 'usb.h'. The usb.h library is provided with the libusb-dev package. Run this command to install:

      sudo apt-get install libusb-dev

      I bet you will be able to compile libhid after that.

      Delete
  2. Thanks for this, i put the mod online

    https://bitbucket.org/sjc4llc/libhid_pi

    ReplyDelete
  3. Matt, I am trying the same project you have been working on. I am able to get all the files downloaded and set up. But, I receive a "cannot execute bianary file" error when trying to run the thumctl -f.

    If I try using sudo thumctl -f, I get a "syntax error: work unexpected (expecting ")")"

    Any ideas?

    -Eric-

    ReplyDelete

Please note all comments are moderated by me before they appear on the site. It may take a day or so for me to get to them. Thanks for your feedback.