CMake将共享库链接到系统版本而不是自己的

时间:2019-04-19 10:06:34

标签: cmake shared-libraries rpath

我在项目的源目录中有自己的libusb版本,可以通过以下方式找到:

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}")

我在哪里成功运行

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

然后我将其链接

target_link_libraries(${TARGET_NAME} ... LibUsb)

我在哪里检查CMake为该目标产生的link.txt

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

但是当检查我的二进制文件时,我得到了

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$

我不明白为什么可执行文件搜索libusb-1.0.so.0而不搜索libusb-1.0.so。我也不明白为什么它会在系统目录而不是我自己的目录中找到它。

我可以以某种方式利用RPATH进行解决吗?最好使用CMake?

0 个答案:

没有答案