Wednesday, December 11, 2013

Problem linking w/ libusb on Linux

Trying to compile the software for my weather station ( http://te923.fukz.org ) on Ubuntu 12.04.3 LTS, I ran into problems like this:

/tmp/ccPvR8eT.o: In function `te923_handle':
te923usb.c:(.text+0xe3): undefined reference to `usb_init'
te923usb.c:(.text+0x132): undefined reference to `usb_open'
te923usb.c:(.text+0x183): undefined reference to `usb_get_driver_np'
te923usb.c:(.text+0x19d): undefined reference to `usb_detach_kernel_driver_np'
te923usb.c:(.text+0x1ae): undefined reference to `usb_set_configuration'
te923usb.c:(.text+0x1f1): undefined reference to `usb_claim_interface'

te923usb.c:(.text+0x231): undefined reference to `usb_set_altinterface'

It took some time figuring out what the problem was. There were references to libusb-0.1 libusb-1.0 and fidderences in linking and calling conventions. There were references to -L for gcc. All os these failed to work.

The problem showed itself to be, that the -llib flag must come after the files that are to be linked with the library, so changing 

te923con: te923con.c te923con.h te923usb.c te923usb.h te923com.c te923com.h
gcc -Wall -lusb -o te923con te923con.c te923usb.c te923com.c

to 

te923con: te923con.c te923con.h te923usb.c te923usb.h te923com.c te923com.h
gcc -Wall -o te923con te923con.c te923usb.c te923com.c -lusb

fixed the problem, and allowed me to compile without errors.

Guess this GCC change will affect a multitude of software.