Undefined symbols in .so after compiling despite supplying libs

13,896

Got it: With gcc the library list must be immediately following the object being linked. From this the reordering of:

gcc -shared `pkg-config libudev --libs` -o libhid.so hid.o

to:

gcc -shared -o libhid.so hid.o `pkg-config libudev --libs`

...now results in properly functioning lib. This was derived from the following answer which was to an unrelated question but still applied to this question:

https://stackoverflow.com/a/10456630/515655

Share:
13,896
user515655
Author by

user515655

Updated on June 04, 2022

Comments

  • user515655
    user515655 almost 2 years

    Attempting to compile .so from hidapi's linux/hid.c but not very experienced with tasks of this nature. Shared lib has undefined symbols but I don't know why they are being left undefined.

    Using said .so from my program gets straightforward error:

    symbol lookup error: libhid.so: undefined symbol: udev_new

    Using a modified version of hidapi's linux makefile:

    all: hidtest
    
    CC       ?= gcc
    CFLAGS   ?= -Wall -fPIC -c -g
    
    CXX      ?= g++
    CXXFLAGS ?= -Wall -fPIC -g
    
    COBJS     = hid.o
    CPPOBJS   = ../hidtest/hidtest.o
    OBJS      = $(COBJS) $(CPPOBJS)
    LIBS      = `pkg-config libudev --libs`
    INCLUDES ?= -I../hidapi `pkg-config libudev --cflags`
    
    
    hidtest: $(OBJS)
        $(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(LIBS) -o hidtest
    
    $(COBJS): %.o: %.c
        $(CC) $(CFLAGS) -c $(INCLUDES) $< -o $@
    
    $(CPPOBJS): %.o: %.cpp
        $(CXX) $(CXXFLAGS) -c $(INCLUDES) $< -o $@
    
    clean:
        rm -f $(OBJS) hidtest
    
    .PHONY: clean
    

    ...which is then built to .so:

    gcc -shared pkg-config libudev --libs -o libhid.so hid.o

    ... my pkg-config output is:

    $ pkg-config libudev --libs
    -ludev -lrt
    

    Confirming the undefined symbols:

    nm libhid.so | grep udev

    000000000000181f t copy_udev_string

                 U udev_device_get_devnode
                 U udev_device_get_parent_with_subsystem_devtype
                 U udev_device_get_sysattr_value
                 U udev_device_new_from_devnum
                 U udev_device_new_from_syspath
                 U udev_device_unref
                 U udev_enumerate_add_match_subsystem
                 U udev_enumerate_get_list_entry
                 U udev_enumerate_new
                 U udev_enumerate_scan_devices
                 U udev_enumerate_unref
                 U udev_list_entry_get_name
                 U udev_list_entry_get_next
                 U udev_new
                 U udev_unref
    

    And udev deps don't show up in the .so:

    $ldd libhid.so   
    linux-vdso.so.1 => (0x00007ffffcffe000) 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc9d03a3000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fc9d099e000)
    

    Thank you