1. Introduction
This article introduces the application development based on libusb for OK3568-C development board, and introduces two ways: building libusb library, linking application to libusb library, and directly compiling libusb source code and application source code together. It also introduces the use of GDB for debugging.
2. How to build the library
2.1 Component Library
Download source code
git clone https://github.com/libusb/libusb.git
Enter the source code path
cd libusb/
Automatic generated
./autogen.sh
Configuration
./configure
Build and Install
make && make install
2.2 Testing
Sample code testing using source code
Enter the sample code path
cd examples/
Compile
make
Running the test program
Make compiles many test programs, such as running listdevs
./listdevs
Print as follows
root@ok3568:~/Desktop/libusb/examples# ./listdevs
1d6b:0003 (bus 8, device 1)
1d6b:0002 (bus 7, device 1)
1d6b:0003 (bus 6, device 1)
1d6b:0002 (bus 5, device 1)
1d6b:0001 (bus 4, device 1)
1d6b:0002 (bus 2, device 1)
1d6b:0001 (bus 3, device 1)
1d6b:0002 (bus 1, device 1)
2.3 Manual compilation
The examples directory above provides a Makefile for compiling all the examples. We can also manually compile our own applications or a sample program. For example, -lusb-1.0 links to the libusb library, -I specifies the header file path, and -L specifies the library file path.
gcc listdevs.c -I../libusb -lusb-1.0 -L../libusb
2.4 Debugging
Install gdb
apt install gdb
-g compile
gcc listdevs.c -I../libusb -lusb-1.0 -L../libusb-g -o listdevs
Debugging with gdb
gdb listdevs
3. Use source code directly
The above method of building the library first and then linking the library does not allow you to see the source code of libusb during debugging. We can directly use the source code of libusb and compile it together with our own application code, which makes it easier to debug and read the source code.
Copy the contents to your own project directory
Copy the following content under libusb
core.c
descriptor.c
hotplug.c
I.c
libusb.h
libusbi.h
sync.c
version.h
version_nano.h
strerror.c
Copy the following content under libusb/os
events_posix.c
events_posix.h
linux_udev.c
linux_usbfs.c
linux_usbfs.h
threads_posix.c
threads_posix.h
listdevs.c under examples
At the same time, copy the config.h file
The final directory is as follows
.
|-- examples
| |-- config.h
| `-- listdevs.c
`-- libusb
|-- core.c
|-- descriptor.c
|-- hotplug.c
|-- i.c
|-- libusb.h
|-- libusbi.h
|-- the
| |-- events_posix.c
| |-- events_posix.h
| |-- linux_udev.c
| |-- linux_usbfs.c
| |-- linux_usbfs.h
| |-- threads_posix.c
| `-- threads_posix.h
|-- strerror.c
|-- sync.c
|-- version.h
`-- version_nano.h
3 directories, 19 files
The compilation command is as follows
cd examples
gcc ../libusb/*.c ../libusb/os/events_posix.c ../libusb/os/linux_udev.c ../libusb/os/linux_usbfs.c ../libusb/os/threads_posix.c ./listdevs.c -I../libusb -I../libusb/os -I./ -lpthread -ludev -g -o listdevs
gcc libusb/*.c libusb/os/events_posix.c libusb/os/linux_udev.c libusb/os/linux_usbfs.c libusb/os/threads_posix.c ./examples/listdevs.c -Ilibusb -Ilibusb/os -Iexamples - lpthread -ludev -g -o listdevs
Debugging with GDB
gdb listdevs
Print as follows
root@ok3568:~/Desktop/libusbtest/examples# gdb listdevs
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from listdevs...
(gdb)
layout srcOpen source view
b main breaks the function in main
(gdb) b main
Breakpoint 1 at 0x178cc: file ./listdevs.c, line 53.
(gdb)
Enter run and press Enter to execute to the breakpoint
Press s to run in single step to enter the source code of libusb for debugging
It is relatively simple to use libusb under Linux. There is not much source code for libusb. In the development stage, it is recommended to compile it directly with the application code for easy debugging. It depends on libpthread libudev.
From the above, we can see that thanks to the powerful performance and perfect environment of OK3568-C, USB application development can be carried out conveniently.