0

I have my own libusb version inside the project's source directory, that I find with:

find_path(LibUsb_INCLUDE_DIR NAMES libusb.h PATHS ${CMAKE_CURRENT_SOURCE_DIR}/LibUsb/Include)
if(NOT LibUsb_INCLUDE_DIR)
    message(FATAL_ERROR "LibUsb: include directory not found")
endif()

find_library(LibUsb_LIBRARY NAMES usb-1.0 libusb-1.0 PATHS ${OUTPUT_BIN_DIR})
if(NOT LibUsb_LIBRARY)
    message(FATAL_ERROR "LibUsb: library not found")
endif()

add_library(LibUsb SHARED IMPORTED GLOBAL)
set_target_properties(LibUsb PROPERTIES IMPORTED_LOCATION ${LibUsb_LIBRARY})
set_target_properties(LibUsb PROPERTIES IMPORTED_IMPLIB ${LibUsb_LIBRARY})
set_target_properties(LibUsb PROPERTIES IMPORTED_NO_SONAME ON)
set_target_properties(LibUsb PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LibUsb_INCLUDE_DIR})

message(STATUS "LibUsb: found at ${LibUsb_LIBRARY}")

This run successfully where I get

-- LibUsb: found at /mnt/c/x/Build/Linux-Debug/Output/Bin/libusb-1.0.so

Then I link it with

target_link_libraries(${TARGET_NAME} ... LibUsb)

Where I inspected link.txt that CMake produces for that target:

/usr/bin/g++-4.8   -Wall -Werror -Wextra -Wno-missing-field-initializers -g ... -lusb-1.0 

But when inspecting my binary I get

gal@GalA-T480:/mnt/c/x$ readelf -d Build/Linux-Debug/Output/Bin/myexe | grep NEEDED
0x0000000000000001 (NEEDED)             Shared library: [libusb-1.0.so.0]
0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
gal@GalA-T480:/mnt/c/x$ ldd Build/Linux-Debug/Output/Bin/myexe
        linux-vdso.so.1 (0x00007fffcd197000)
        libusb-1.0.so.0 => /lib/x86_64-linux-gnu/libusb-1.0.so.0 (0x00007f1a49930000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1a495a0000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1a49380000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f1a49160000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1a48d60000)
        libudev.so.1 => /lib/x86_64-linux-gnu/libudev.so.1 (0x00007f1a48b40000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f1a48790000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f1a49c00000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f1a48580000)
gal@GalA-T480:/mnt/c/x$

I don't get why the executable searches for libusb-1.0.so.0 and not libusb-1.0.so. I also don't get why it finds it at system directories and not my own.

Can I utilize RPATH somehow to work it out? Preferably using CMake?

galah92
  • 3,621
  • 2
  • 29
  • 55
  • It seems that it is setup to use system libraries because of setting `IMPORTED_NO_SONAME` and not setting `RPATH`. The `link.txt` line looks incorrect to me because it should supply the full path name, unless both libusb.so have the same interface and you don't care. As for using RPATH you can start here: https://cmake.org/cmake/help/v3.14/prop_tgt/BUILD_RPATH.html – fdk1342 Apr 19 '19 at 13:48
  • @Fred Thanks, I'd like to tackle the relative path in `link.txt`. Do you have any idea what's the cause of that? I agree it should point to absolute path as that's what `find_library` is finding. – galah92 Apr 19 '19 at 13:51
  • It seems that happens with `IMPORTED_NO_SONAME` `ON` as stated in the documentation. – fdk1342 Apr 19 '19 at 13:59
  • @Fred with `OFF` I'm getting `-lusb-1.0` in `link.txt`. I also `set(CMAKE_BUILD_RPATH $ORIGIN)` and got `-Wl,-rpath,"\$ORIGIN:/mnt/x/...` which looks better. But `ldd` still finds the wrong one. – galah92 Apr 20 '19 at 15:22

0 Answers0