0.libusb的介绍:参考[1]
1.环境:vmware_fedora_10(linux-2.6.x)
2.获取源代码:http://sourceforge.net/projects/libusb/ (最好选择libusb-1.0.9版本,下载次数最多,自是有它的道理)
3.解压源码tar xjvf libusb-1.0.9.tar.bz2 按照INSTALL文件给出的提示进行安装:主要分为./configure ->make -> make install
4.安装过程如下:
configure略去......(看不懂......)
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
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设备)
- #include <stdio.h>
- #include <sys/types.h>
- #include <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) ",
- desc.idVendor, desc.idProduct,
- libusb_get_bus_number(dev), libusb_get_device_address(dev));
- }
- }
- int main(void)
- {
- 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;
- }
#include <stdio.h> #include <sys/types.h> #include <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) ", desc.idVendor, desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev)); } } int main(void) { 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; }
编译的时候我们可以用自带的makefile生成可执行文件,也可以用如下操作进行:(参考[2])
[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 7.若想打开调试开关,看出libusb内部的运行情况
在libusb/libusbi.h中有如下代码
- #ifdef ENABLE_DEBUG_LOGGING
- #define usbi_dbg(...) _usbi_log(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__)
- #else
- #define usbi_dbg(...) do {} while(0)
- #endif
#ifdef ENABLE_DEBUG_LOGGING #define usbi_dbg(...) _usbi_log(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__) #else #define usbi_dbg(...) do {} while(0) #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
http://baike.baidu.com/view/4598042.htm
[2].解决编译问题:http://libusb.6.n5.nabble.com/Compile-my-program-td2645564.html