ROS Lunar中使用libusb的旧C++程序编译报错问题咨询
Hey there! Let’s work through this problem step by step—running an old C++ joystick program in ROS Lunar that relies on libusb can be tricky, but we’ll get it sorted.
1. Fix the Header Include Path
First off, your original include line #include "libusb/libusb.h" is likely the culprit here. On most systems (including Ubuntu Xenial, which ROS Lunar targets), the libusb-1.0 development headers live in /usr/include/libusb-1.0/.
So you’ll need to update your include to:
#include <libusb-1.0/libusb.h>
To double-check where your system stores the header, run this command in the terminal:
find /usr/include -name "libusb.h"
This should return something like /usr/include/libusb-1.0/libusb.h, confirming the correct path.
2. Correct Your CMakeLists.txt Configuration
Since you mentioned your CMakeLists is causing compilation errors, here’s a standard, ROS-compatible setup to link libusb properly. Replace or update your existing config with this:
cmake_minimum_required(VERSION 2.8.3) project(your_package_name) # Use pkg-config to locate libusb-1.0 find_package(PkgConfig REQUIRED) pkg_check_modules(LIBUSB REQUIRED libusb-1.0) # ROS dependencies (adjust based on your package's needs) find_package(catkin REQUIRED COMPONENTS roscpp # add other ROS packages you depend on here ) # Include directories for ROS and libusb include_directories( ${catkin_INCLUDE_DIRS} ${LIBUSB_INCLUDE_DIRS} ) # Build your executable add_executable(joystick_node src/your_program.cpp) # Link against ROS libraries and libusb target_link_libraries(joystick_node ${catkin_LIBRARIES} ${LIBUSB_LIBRARIES} )
What each part does:
pkg_check_modules(LIBUSB REQUIRED libusb-1.0): This finds the libusb-1.0 library and its header paths using the system’s pkg-config files.${LIBUSB_INCLUDE_DIRS}: Adds the libusb header directory to your include path, so the compiler can find the corrected header.${LIBUSB_LIBRARIES}: Links your executable against the libusb shared library.
3. Install Missing Dependencies
Make sure you have the libusb-1.0 development package installed on your system. Run this command:
sudo apt-get update && sudo apt-get install libusb-1.0-0-dev
ROS Lunar runs on Ubuntu 16.04 (Xenial), so this package is available in the default repos.
4. Handle USB Device Permissions (Critical for Runtime)
Even if you compile successfully, you might hit permission issues when trying to read the joystick via USB. To avoid running your ROS node with sudo, create a udev rule:
- Create a new rule file:
sudo nano /etc/udev/rules.d/99-joystick-usb.rules - Add this line (replace
xxxxandyyyywith your joystick’s vendor and product ID—find these withlsusb):SUBSYSTEM=="usb", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", MODE="0666" - Reload udev rules to apply changes:
sudo udevadm control --reload-rules && sudo udevadm trigger
5. Debugging Tips
If you still get compilation errors, share the exact error message (e.g., "header not found" or "undefined reference to libusb function")—that’ll help narrow things down. Common issues include:
- Forgetting to add
${LIBUSB_INCLUDE_DIRS}toinclude_directories - Missing
${LIBUSB_LIBRARIES}intarget_link_libraries - Typing errors in the package name or file paths
内容的提问来源于stack exchange,提问作者G-Pereira

