前言
之前把家里的库存小模块都玩了一遍,这些主要是配在单片机玩的,但是我放到linux上,目的就是学习linux自己写驱动。至于usb等驱动包括v4l2内核配置下就有了。然后网上找的简单的框架API调用下,家里的usb camera就用起来了。2年前我就已经玩过v4l2了,arm-VS2017 opencv远程人脸识别--APPLE的学习笔记,至于libusb我去移植它还是第一次,我想后面做些usb相关的应用,所以看了下libusb是常用库,所以就交叉编译移植下。代码已上传我的gitee,15_usbdev工程。
问题
包括交叉编译问题的临时解决,及测试代码编译后segment fault的调试解决
(如下6个步骤不要参考,参考正确的libusb交叉编译步骤)
一,libusb交叉编译
1.交叉编译先./autogen.sh。
2.配置
./configure --build=i686-linux --host=arm-linux --prefix=/home/applecai/tools/install CC=/home/applecai/bbb/ti-processor-sdk-linux-am335x-evm-06.01.00.08/linux-devkit/sysroots/x86_64-arago-linux/usr/bin/arm-linux-gnueabihf-gcc CXX=/home/applecai/bbb/ti-processor-sdk-linux-am335x-evm-06.01.00.08/linux-devkit/sysroots/x86_64-arago-linux/usr/bin/arm-linux-gnueabihf-g++
结果报错
打开config.log查看是-V的问题,改成-v。
又报错4447,就是如下2句,我就删除了。
/LT_INIT/
/LT_LANG(Windows Resource)/
又报错error: cannot find input file: `Makefile.in',按网上搜索的结果
就在configure 之前执行如下命令
aclocal
libtoolize --force
automake --add-missing
autoconf
autoheader
make clean
重新再运行步骤2的配置命令,成功生成makefile
make
make install
然后拿libusb中的example code来使用,功能就是遍历usb设备。
静态编译由于缺少udev库报错。于是动态编译,如下命令是错误的,-fPIC -shared是编译so动态库的。但是我一开始没反应过来
arm-linux-gnueabihf-gcc -g -fPIC -shared usbtest.c -o usbtest -I/home/applecai/tools/install/include/libusb-1.0/ -L/home/applecai/tools/install/lib/ -lusb-1.0
直接运行编译后的代码则segment fault
通过开发板上gdb调试,由于当前是buildroot uclib编译的,我之前的gdb是用的自己通过busybox glib编译的,所以换了挂载的文件系统,然后里面有我之前准备的gdb bin文件。gdb运行后提示缺少python。查看报错是有个路径也要copy gdb文件夹。copy完成后gdb正常运行。发现原因是0x00000000 in ?? ()
[ 15.766487] random: gdb: uninitialized urandom read (24 bytes read)
GNU gdb (GDB) 8.2
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./usbtest...done.
(gdb) b main
Breakpoint 1 at 0x8dc: file usbtest.c, line 39.
(gdb) r
Starting program: /usr/study/usbtest
Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
(gdb)
这时候反应到我添加了-fPIC -shared,重新改成动态编译,依然报错,缺少udev。网上搜索了下libusb默认找设备是从udev,可以通过配置--disable udev来禁止调用udev相关函数。所以我重新编译libusb,结果同样的命令再运行一次,configure无法通过。于是重新unzip libusb,本次编译就非常顺利。之前可能是一开始编译不正确,后来没有用make clean。所以如下才是正确的
正确的libusb交叉编译步骤
./autogen.sh
./config