折腾了2天,为了一个嵌入式Linux的老应用需要libusb库访问Cypress usb设备,写出来给大家参考:
平台: Ubuntu 13.10 64bit
准备资料: ARM交叉编译工具链已经安装好,并export到PATH中(arm-linux-gcc... )
1.下载libusb-1.0.9软件包 http://sourceforge.net/projects/libusb/files/
libusb-compat-0.1.4软件包
!!libusb-1.0.9的版本,编译出的include里面没有以前写的应用程序需要的usb.h文件!!! 需要在编译1.0版本后,额外编译0.1 compat兼容支持包,它会产生usb.h!!
(libusb分为1.0版和0.1版两种版本,1.0并不向下兼容0.1,需要libusb-compat的支持;因此,你可以同时装上1.0和0.1,但不可同时装0.1版和libusb-compat。)
2.解压
3.进入libusb-1.0.9和libusb-compat-0.1.4,分别先后交叉编译:
1) 编译libusb-1.0.9:
mkdir install
CROSS_COMPILE=1 CC=arm-linux-gcc CXX=arm-linux-g++ ./configure --build=i686-linux --host=arm-linux --prefix=`pwd`/install
(./configure --build=i686-linux --host=arm-linux --prefix=/usr/local/arm/3.4/libusb/ CC=arm-linux-gcc CXX=arm-linux-g++)
make
make install
说明一下:--build=i686-linux表示该软件在x86平台上被编译
--host=arm-linux表示该软件编译完成后在arm平台上运行
--prefix后面为软件安装目录
检验编译结果是否就是ARM平台可用的库:
root@ubuntu:/home/jerry/libusb-1.0.9/install/lib# file libusb-1.0.so.0.1.0
libusb-1.0.so.0.1.0: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked, not stripped
查看.so库中可用的库函数:
root@ubuntu:/home/jerry/libusb-1.0.9/install/lib# nm libusb-1.0.so.0.1.0
00004824 T libusb_lock_event_waiters
00003304 T libusb_open
00003404 T libusb_open_device_with_vid_pid
...都是libusb函数,没有usb开头的函数
各位觉得以上原创资料有用的话,支持下我的照片呗:
http://www.microfotos.com/?p=home_usershow&userid=34409
(make distclean通常用于删除编译目标的设定)
libusb INSTALL 文档内介绍, host=TYPE 指的是 *使用* 交叉编译工具编译产生目标平台的文件;
target=TYPE 指的是编译出的编译工具链,将 *为目标平台* 编译产生文件.
2)编译libusb-compat-0.1.4(依赖于libusb-1.0.4的编译结果,故configure时需要额外指定其PKG_CONFIG_PATH):
mkdir install
CROSS_COMPILE=1 CC=arm-linux-gcc CXX=arm-linux-g++ ./configure --build=i686-linux --host=arm-linux --prefix=`pwd`/install PKG_CONFIG_PATH=/home/jerry/libusb-1.0.9/install/lib/pkgconfig
make
make install
检验编译结果是否就是ARM平台可用的库:
root@ubuntu:/home/jerry/libusb-compat-0.1.4/install/lib# file libusb-0.1.so.4.4.4
libusb-0.1.so.4.4.4: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked, not stripped
查看.so库中可用的库函数:
root@ubuntu:/home/jerry/libusb-compat-0.1.4/install/lib# nm libusb-0.1.so.4.4.4
U libusb_kernel_driver_active
U libusb_open
U libusb_ref_device
...
00001750 t usbi_log
00002f6c T usb_init
00001f94 T usb_open
有libusb函数,也有usb开头的函数
附:libusb 0.1.4 compat找不到依赖的libusb-1.0.4的configure出错:
查看configure.log,错误是libusb-1.0未找到,需要在PKG_CONFIG_PATH环境变量中加入已编译好的libusb-1.0的libusb-1.0.pc路径
configure:10684: checking for arm-linux-pkg-config
configure:10717: result: no
configure:10727: checking for pkg-config
configure:10745: found /usr/bin/pkg-config
configure:10757: result: /usr/bin/pkg-config
configure:10769: WARNING: using cross tools not prefixed with host triplet
configure:10782: checking pkg-config is at least version 0.9.0
configure:10785: result: yes
configure:10795: checking for LIBUSB_1_0
configure:10802: $PKG_CONFIG --exists --print-errors "libusb-1.0 >= 0.9.1"
Package libusb-1.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libusb-1.0.pc‘
to the PKG_CONFIG_PATH environment variable
No package ‘libusb-1.0‘ found
后记:
应用程序链接的时候,仅链接libusb compat 0.1.4的so库,说还缺少库函数,又把libusb 1.0.9的so库加上,才链接成功。
编译的时候记得加上-lusb选项,交叉编译还要加上libusb包的路径-I/.../include和-L/.../lib
--host=arm-linux -I../libusb-compat-0.1.4/install/include -L../libusb-compat-0.1.4/install/lib
CC = arm-linux-gcc
CPP = arm-linux-g++
# Linux configuration
else
SUFFIX = so
LIBBASENAME = libseabreeze
CFLAGS_BASE += -I/home/jerry/libusb-compat-0.1.4/install/include
LFLAGS_APP += -L/usr/lib \
-L/home/jerry/libusb-compat-0.1.4/install/lib \
-L/home/jerry/libusb-1.0.9/install/lib \
-lusb -lusb-1.0\
!!!!新增上述两个libusb的 so库
make ---> DONE!