Pergunta

We're trying to compile some code on an Ubuntu box that requires libusb but we keep getting "undefined reference to" errors when it tries to link against it.

The code in question is this one: https://gitorious.org/minibox-dcdcusb/minibox-dcdcusb

It requires libusb so I install that:

apt-get install libusb libusb-dev

Then I run make and get the following errors:

➜  minibox-dcdcusb git:(master) ✗ make                    
cc -Wall  main.c -o dcdc-usb `pkg-config --libs libusb` -lm -L. -ldcdc-usb
./libdcdc-usb.so: undefined reference to `usb_get_driver_np'
./libdcdc-usb.so: undefined reference to `usb_interrupt_read'
./libdcdc-usb.so: undefined reference to `usb_find_busses'
./libdcdc-usb.so: undefined reference to `usb_find_devices'
./libdcdc-usb.so: undefined reference to `usb_set_configuration'
./libdcdc-usb.so: undefined reference to `usb_interrupt_write'
./libdcdc-usb.so: undefined reference to `usb_control_msg'
./libdcdc-usb.so: undefined reference to `usb_set_altinterface'
./libdcdc-usb.so: undefined reference to `usb_init'
./libdcdc-usb.so: undefined reference to `usb_set_debug'
./libdcdc-usb.so: undefined reference to `usb_open'
./libdcdc-usb.so: undefined reference to `usb_claim_interface'
./libdcdc-usb.so: undefined reference to `usb_detach_kernel_driver_np'
./libdcdc-usb.so: undefined reference to `floor'
./libdcdc-usb.so: undefined reference to `usb_get_busses'
collect2: error: ld returned 1 exit status
make: *** [dcdc-usb] Error 1

I have verified that pkg-config is reporting the proper config params:

➜  minibox-dcdcusb git:(master) ✗ pkg-config --libs libusb
-lusb  

And I've made sure that the binaries are where they're supposed to be:

➜  minibox-dcdcusb git:(master) ✗ ls -l /usr/lib/x86_64-linux-gnu/libusb*
lrwxrwxrwx 1 root root    37 des  3 13:58 /usr/lib/x86_64-linux-gnu/libusb-0.1.so.4 -> /lib/x86_64-linux-gnu/libusb-0.1.so.4
-rw-r--r-- 1 root root 42998 des  3 13:58 /usr/lib/x86_64-linux-gnu/libusb.a
lrwxrwxrwx 1 root root    41 des  3 13:58 /usr/lib/x86_64-linux-gnu/libusb.so -> /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4

And I've even made sure that the references that the linker seems to be complaining about are actually in the libusb binaries:

(spoiler: all the symbols that the linker complains about seem to be there)

Finally, I ran the compile command with -Wl,--verbose to see exactly what binaries the linker was picking up and it seems that it is in fact picking up the correct binaries but it just isn't able to link against them. Here is the output of that:

I'm quite lost as to what could possibly be the problem here? Why isn't the linker able to link against libusb?

Foi útil?

Solução

Why isn't the linker able to link against libusb?

It is able to, but when the linker sees that library it doesn't have any undefined references to symbols in libusb, so it just moves on and ignores it. Later the linker sees libdcdc-usb, which has undefined references to symbols in libusb, but it's too late by then, the linker has already closed libusb and stopped looking at it.

As the comment says, order of linker arguments matters, but the comment suggests moving them earlier, which is the wrong way round.

In fact I read somewhere that the linker information should come after the source argument that requires that library.

It's not just "linker arguments after source files" you need to order the libraries so that libraries that provide symbol definitions come after the objects that need those definitions.

libdcdc-usb depends on libusb so libusb has to come later, so move the pkg-config call to the end.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top