我正在使用 Ubuntu 22.04 和 CLion 在 C: 中运行它
#include <stdio.h>
#include <sys/types.h>
#include <libusb-1.0/libusb.h>
static void print_devs(libusb_device **devs)
{
libusb_device *dev;
int i = 0;
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
fprintf(stderr, "failed to get device descriptor");
return;
}
printf("%04x:%04x (bus %d, device %d)\n",
desc.idVendor, desc.idProduct,
libusb_get_bus_number(dev), libusb_get_device_address(dev));
}
}
int main(void)
{
printf("hello");
libusb_device **devs;
int r;
ssize_t cnt;
r = libusb_init(NULL);
if (r < 0)
return r;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
return (int) cnt;
print_devs(devs);
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return 0;
}
这是
test.c
的 libusb
。但是,当 libus-dev
和 libusb-1.0.0-dev
都下载时,我一直收到此错误:
====================[ Build | usbTool | Debug-System ]==========================
/snap/clion/219/bin/cmake/linux/bin/cmake --build /home/wonster/CLionProjects/usb-tool/cmake-build-debug-system --target usbTool -- -j 6
[ 50%] Building C object CMakeFiles/usbTool.dir/test.c.o
[100%] Linking C executable usbTool
/usr/bin/ld: CMakeFiles/usbTool.dir/test.c.o: in function `print_devs':
/home/wonster/CLionProjects/usb-tool/test.c:12: undefined reference to `libusb_get_device_descriptor'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:20: undefined reference to `libusb_get_device_address'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:20: undefined reference to `libusb_get_bus_number'
/usr/bin/ld: CMakeFiles/usbTool.dir/test.c.o: in function `main':
/home/wonster/CLionProjects/usb-tool/test.c:31: undefined reference to `libusb_init'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:35: undefined reference to `libusb_get_device_list'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:40: undefined reference to `libusb_free_device_list'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:42: undefined reference to `libusb_exit'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/usbTool.dir/build.make:97: usbTool] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/usbTool.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/usbTool.dir/rule] Error 2
gmake: *** [Makefile:124: usbTool] Error 2
我只是不知道原因以及如何解决这个问题。我在互联网上关注了很多博客,但没有帮助。
我猜测CMake文件是否有问题:
cmake_minimum_required(VERSION 3.22)
project(usbTool C)
set(CMAKE_C_STANDARD 99)
#声明头文件路径
#set(INC_DIR /opt/homebrew/Cellar/libusb/1.0.26/include/libusb-1.0)//绝对路径
set(INC_DIR ./include)
#声明链接库路径
#set(LINK_DIR /opt/homebrew/Cellar/libusb/1.0.26/lib)//绝对路径
set(LINK_DIR ./lib)
#引入头文件
include_directories(${INC_DIR})
#引入库文件
link_directories(${LINK_DIR})
add_executable(usbTool test.c)
#引入第三方库
target_link_libraries(usbTool libusb-1.0.a)
这是项目内容:
任何帮助都是非凡的!预先感谢!
据我刚刚发现,在/usr/lib64中,libusb.so是一个符号链接,指向libusb-0.1.so.4.4.4,这是一个非常旧的libusb,尽管我有libusb-1.0.so。 0.3.0 也在同一目录中。 作为 root,我删除了链接,然后链接了 libusb.so -> libusb-1.0.so.0.3.0,一切正常。 查看 /usr/lib[64] 了解具体信息。