Don't mix LDFLAGS and LDLIBS

Compilation of kinect_upload_fw (from kinect-audio-setup) was failing on some systems with messages like:

cc -lusb-1.0    kinect_upload_fw.o   -o kinect_upload_fw
kinect_upload_fw.o: In function `get_reply':
kinect_upload_fw.c:(.text+0x8b): undefined reference to `libusb_bulk_transfer'
kinect_upload_fw.o: In function `main':
kinect_upload_fw.c:(.text.startup+0x76): undefined reference to `libusb_init'
kinect_upload_fw.c:(.text.startup+0x82): undefined reference to `libusb_set_debug'
kinect_upload_fw.c:(.text.startup+0x93): undefined reference to `libusb_open_device_with_vid_pid'
kinect_upload_fw.c:(.text.startup+0xb5): undefined reference to `libusb_set_configuration'
kinect_upload_fw.c:(.text.startup+0xc3): undefined reference to `libusb_claim_interface'
kinect_upload_fw.c:(.text.startup+0x151): undefined reference to `libusb_bulk_transfer'
kinect_upload_fw.c:(.text.startup+0x189): undefined reference to `libusb_close'
kinect_upload_fw.c:(.text.startup+0x190): undefined reference to `libusb_exit'
kinect_upload_fw.c:(.text.startup+0x1f3): undefined reference to `libusb_bulk_transfer'
kinect_upload_fw.c:(.text.startup+0x2f4): undefined reference to `libusb_bulk_transfer'
kinect_upload_fw.c:(.text.startup+0x345): undefined reference to `libusb_bulk_transfer'
kinect_upload_fw.c:(.text.startup+0x469): undefined reference to `libusb_bulk_transfer'
collect2: ld returned 1 exit status
make[1]: *** [kinect_upload_fw] Error 1

even though libusb-1.0 was correctly installed on the system.

This was happening because I was putting the -lusb-1.0 option into LDFLAGS. Well, this is not how you are supposed to do it, especially when you rely on implicit rules of GNU Make, you must use LDLIBS (or the equivalent LOADLIBES) instead.

Here are the facts of the issue:

  • When some options, like -Wl,--as-needed, are passed to the compiler (and then to the linker) the order of linking options becomes important: libraries must be specified after the objects in the linking command.
  • I was specifying the libraries into LDFLAGS:
    LDFLAGS += $(shell pkg-config --libs libusb-1.0)
    
  • The implicit Make rule generates something like this:
    LINK.o = $(CC) $(LDFLAGS) $(TARGET_ARCH)
    kinect_upload_fw: kinect_upload_fw.o
    	$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@
    
    where LDFLAGS is expanded before anything else, and the linker couldn't figure out the libraries to use for linking; note that LDLIBS comes after $^ which is the automatic variable expanded to the prerequisites of the rule (the objects, namely kinect_upload_fw.o in this case).

A fix for kinect_upload_fw has been pushed in 863d1b3.


CommentsSyndicate content

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
B
x
m
L
3
T
Enter the code without spaces.