linux 에서 libusb 설치 및 테스트

10546 단어 Libusb
@2013/5/14
0. libusb 의 소개: 참고 [1]
1. 환경: vmwarefedora_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 의 동작 은 주로 libusb 의 소스 코드 를 컴 파일 합 니 다 * /
[root@zx libusb-1.0.9]# makemake  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 installMaking 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 디 렉 터 리 에 추가 하 였 음 을 알려 줍 니 다. 따라서 라 이브 러 리 를 기반 으로 프로 그래 밍 할 때 이 라 이브 러 리 를 포함 해 야 합 니 다 * /
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 
#include 
#include 

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  /* lsusb 명령 과 일치 * / Bus 001 Device 003: ID 0cf 3: 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 에는 다음 과 같은 코드 가 있 습 니 다.
#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
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 소개:http://baike.baidu.com/view/4598042.htm
[2]. 컴 파일 문제 해결:http://libusb.6.n5.nabble.com/Compile-my-program-td2645564.html

좋은 웹페이지 즐겨찾기