Chinaunix首页 | 论坛 | 博客
  • 博客访问: 254074
  • 博文数量: 55
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 419
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-06 20:22
文章分类

全部博文(55)

文章存档

2014年(55)

我的朋友

分类: 嵌入式

2014-06-18 13:26:07

@2013/5/14

0.libusb的介绍:参考

1.环境:vmware_fedora_10(linux-2.6.x)

2.获取源代码:  (最好选择libusb-1.0.9版本,下载次数最多,自是有它的道理)

3.解压源码tar xjvf libusb-1.0.9.tar.bz2 按照INSTALL文件给出的提示进行安装:主要分为./configure ->make -> make install

4.安装过程如下:

configure略去......(看不懂......)

/* make的动作主要是编译libusb的源代码 */

[root@zx libusb-1.0.9]# make
make  all-recursive
make[1]: Entering directory `/share/libusb-1.0.9'
Making all in libusb
make[2]: Entering directory `/share/libusb-1.0.9/libusb'
  CC     libusb_1_0_la-core.lo
  CC     libusb_1_0_la-descriptor.lo
  CC     libusb_1_0_la-io.lo
  CC     libusb_1_0_la-sync.lo
  CC     libusb_1_0_la-linux_usbfs.lo
  CC     libusb_1_0_la-threads_posix.lo
  CCLD   libusb-1.0.la
make[2]: Leaving directory `/share/libusb-1.0.9/libusb'
Making all in doc
make[2]: Entering directory `/share/libusb-1.0.9/doc'
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/share/libusb-1.0.9/doc'
make[2]: Entering directory `/share/libusb-1.0.9'
make[2]: Nothing to be done for `all-am'.
make[2]: Leaving directory `/share/libusb-1.0.9'
make[1]: Leaving directory `/share/libusb-1.0.9'

/* make install的动作主要是编译出libusb库并加入到系统文件夹下 */
[root@zx libusb-1.0.9]# make install
Making install in libusb
make[1]: Entering directory `/share/libusb-1.0.9/libusb'
make[2]: Entering directory `/share/libusb-1.0.9/libusb'
test -z "/usr/local/lib" || /bin/mkdir -p "/usr/local/lib"
 /bin/sh ../libtool   --mode=install /usr/bin/install -c   libusb-1.0.la '/usr/local/lib'
libtool: install: /usr/bin/install -c .libs/libusb-1.0.so.0.1.0 /usr/local/lib/libusb-1.0.so.0.1.0
libtool: install: (cd /usr/local/lib && { ln -s -f libusb-1.0.so.0.1.0 libusb-1.0.so.0 || { rm -f libusb-1.0.so.0 && ln -s libusb-1.0.so.0.1.0 libusb-1.0.so.0; }; })
libtool: install: (cd /usr/local/lib && { ln -s -f libusb-1.0.so.0.1.0 libusb-1.0.so || { rm -f libusb-1.0.so && ln -s libusb-1.0.so.0.1.0 libusb-1.0.so; }; })
libtool: install: /usr/bin/install -c .libs/libusb-1.0.lai /usr/local/lib/libusb-1.0.la
libtool: install: /usr/bin/install -c .libs/libusb-1.0.a /usr/local/lib/libusb-1.0.a
libtool: install: chmod 644 /usr/local/lib/libusb-1.0.a
libtool: install: ranlib /usr/local/lib/libusb-1.0.a
libtool: finish: PATH="/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/opt/EmbedSky/4.3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/lib /* 这里提示已经将库加入到/usr/local/lib目录,所以我们基于libusb编程的时候,需要包含这个库 */

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
test -z "/usr/local/include/libusb-1.0" || /bin/mkdir -p "/usr/local/include/libusb-1.0"
 /usr/bin/install -c -m 644 libusb.h '/usr/local/include/libusb-1.0'
make[2]: Leaving directory `/share/libusb-1.0.9/libusb'
make[1]: Leaving directory `/share/libusb-1.0.9/libusb'
Making install in doc
make[1]: Entering directory `/share/libusb-1.0.9/doc'
make[2]: Entering directory `/share/libusb-1.0.9/doc'
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/share/libusb-1.0.9/doc'
make[1]: Leaving directory `/share/libusb-1.0.9/doc'
make[1]: Entering directory `/share/libusb-1.0.9'
make[2]: Entering directory `/share/libusb-1.0.9'
make[2]: Nothing to be done for `install-exec-am'.
test -z "/usr/local/lib/pkgconfig" || /bin/mkdir -p "/usr/local/lib/pkgconfig"
 /usr/bin/install -c -m 644 libusb-1.0.pc '/usr/local/lib/pkgconfig'
make[2]: Leaving directory `/share/libusb-1.0.9'
make[1]: Leaving directory `/share/libusb-1.0.9'

5.源码目录下有example目录,是libusb提供的测试程序listdev.c (列出usb设备)

  1. #include   
  2. #include   
  3. #include   
  4.   
  5. static void print_devs(libusb_device **devs)  
  6. {  
  7.     libusb_device *dev;  
  8.     int i = 0;  
  9.   
  10.     while ((dev = devs[i++]) != NULL) {  
  11.         struct libusb_device_descriptor desc;  
  12.         int r = libusb_get_device_descriptor(dev, &desc);  
  13.         if (r < 0) {  
  14.             fprintf(stderr, "failed to get device descriptor");  
  15.             return;  
  16.         }  
  17.   
  18.         printf("%04x:%04x (bus %d, device %d)\n",  
  19.             desc.idVendor, desc.idProduct,  
  20.             libusb_get_bus_number(dev), libusb_get_device_address(dev));  
  21.     }  
  22. }  
  23.   
  24. int main(void)  
  25. {  
  26.     libusb_device **devs;  
  27.     int r;  
  28.     ssize_t cnt;  
  29.   
  30.     r = libusb_init(NULL);  
  31.     if (r < 0)  
  32.         return r;  
  33.   
  34.     cnt = libusb_get_device_list(NULL, &devs);  
  35.     if (cnt < 0)  
  36.         return (int) cnt;  
  37.   
  38.     print_devs(devs);  
  39.     libusb_free_device_list(devs, 1);  
  40.   
  41.     libusb_exit(NULL);  
  42.     return 0;  
  43. }  

编译的时候我们可以用自带的makefile生成可执行文件,也可以用如下操作进行:(参考)

[root@zx examples]# gcc -I/usr/local/include/libusb-1.0 listdevs.c -L/usr/local/lib -lusb-1.0

-I包含头文件 ;-L链接库

6.listdev的运行结果如下:

[root@zx examples]# gcc -I/usr/local/include/libusb-1.0 listdevs.c -L/usr/local/lib -lusb-1.0
[root@zx examples]# ./a.out
1d6b:0002 (bus 1, device 1)
1d6b:0001 (bus 2, device 1)
0e0f:0003 (bus 2, device 2)
0e0f:0002 (bus 2, device 3)
0cf3:1006 (bus 1, device 3)
[root@zx examples]# lsusb  /* 与lsusb命令得出的是一致 */
Bus 001 Device 003: ID 0cf3:1006 Atheros Communications, Inc.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 003: ID 0e0f:0002 
Bus 002 Device 002: ID 0e0f:0003 
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hubReference:

7.若想打开调试开关,看出libusb内部的运行情况

在libusb/libusbi.h中有如下代码

  1. #ifdef ENABLE_DEBUG_LOGGING  
  2. #define usbi_dbg(...) _usbi_log(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__)  
  3. #else  
  4. #define usbi_dbg(...) do {} while(0)  
  5. #endif  

所以说在makefile加上-DENABLE_DEBUG_LOGGING=1这个编译选项即可.

在libusb的目录下找到makefile(configure之后才会生成),找到CFLAGS = -g -O2,替换为CFLAGS = -g -O2 -DENABLE_DEBUG_LOGGING=1

再次make和make install,切换到之前的listdev程序,可以看见已经打出libusb内部的调用顺序:

[root@zx examples]# ./listdevs
libusb: 0.000000 debug [libusb_init] libusb-1.0.9
libusb: 0.003326 debug [find_usbfs_path] found usbfs at /dev/bus/usb
libusb: 0.004064 debug [op_init] found usb devices in sysfs
libusb: 0.004939 debug [usbi_add_pollfd] add fd 3 events 1
libusb: 0.005519 debug [usbi_io_init] using timerfd for timeouts
libusb: 0.006147 debug [usbi_add_pollfd] add fd 5 events 1
libusb: 0.006710 debug [libusb_init] created default context
libusb: 0.007273 debug [libusb_get_device_list]
libusb: 0.011264 debug [sysfs_scan_device] scan usb1
libusb: 0.012133 debug [sysfs_scan_device] bus=1 dev=1
libusb: 0.012681 debug [enumerate_device] busnum 1 devaddr 1 session_id 257
libusb: 0.013244 debug [enumerate_device] allocating new device for 1/1 (session 257)
libusb: 0.013956 debug [sysfs_scan_device] scan usb2
libusb: 0.014740 debug [sysfs_scan_device] bus=2 dev=1
libusb: 0.015685 debug [enumerate_device] busnum 2 devaddr 1 session_id 513
libusb: 0.016251 debug [enumerate_device] allocating new device for 2/1 (session 513)
libusb: 0.016984 debug [sysfs_scan_device] scan 2-1
libusb: 0.017761 debug [sysfs_scan_device] bus=2 dev=2
libusb: 0.018467 debug [enumerate_device] busnum 2 devaddr 2 session_id 514
libusb: 0.018988 debug [enumerate_device] allocating new device for 2/2 (session 514)
libusb: 0.019789 debug [sysfs_scan_device] scan 2-2
libusb: 0.020473 debug [sysfs_scan_device] bus=2 dev=3
libusb: 0.021145 debug [enumerate_device] busnum 2 devaddr 3 session_id 515
libusb: 0.021742 debug [enumerate_device] allocating new device for 2/3 (session 515)
libusb: 0.022440 debug [sysfs_scan_device] scan 1-1
libusb: 0.023203 debug [sysfs_scan_device] bus=1 dev=3
libusb: 0.023986 debug [enumerate_device] busnum 1 devaddr 3 session_id 259
libusb: 0.024548 debug [enumerate_device] allocating new device for 1/3 (session 259)
libusb: 0.025266 debug [libusb_get_device_descriptor]
1d6b:0002 (bus 1, device 1)
libusb: 0.026017 debug [libusb_get_device_descriptor]
1d6b:0001 (bus 2, device 1)
libusb: 0.026479 debug [libusb_get_device_descriptor]
0e0f:0003 (bus 2, device 2)
libusb: 0.026516 debug [libusb_get_device_descriptor]
0e0f:0002 (bus 2, device 3)
libusb: 0.026576 debug [libusb_get_device_descriptor]
0cf3:1006 (bus 1, device 3)
libusb: 0.026616 debug [libusb_unref_device] destroy device 1.1
libusb: 0.026636 debug [libusb_unref_device] destroy device 2.1
libusb: 0.026652 debug [libusb_unref_device] destroy device 2.2
libusb: 0.026667 debug [libusb_unref_device] destroy device 2.3
libusb: 0.026682 debug [libusb_unref_device] destroy device 1.3
libusb: 0.026698 debug [libusb_exit]
libusb: 0.026731 debug [libusb_exit] destroying default context
libusb: 0.026747 debug [usbi_remove_pollfd] remove fd 3
libusb: 0.026778 debug [usbi_remove_pollfd] remove fd 5

PS:这里还有一个问题:

[root@zx test]# ./a.out
./a.out: error while loading shared libraries: libusb-1.0.so.0: cannot open shared object file: No such file or directory
[root@zx test]# /* 运行的时候若出现这个错误,只要先运行下面一句话即可 */
[root@zx test]# export LD_LIBRARY_PATH=/usr/local/lib

 

Reference:

[1].libusb介绍:

[2].解决编译问题:
转 http://blog.csdn.net/zhengxianghdu/article/details/8925602

阅读(6848) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~