Raspbian(树莓派3)下libFTDI自定义项目GCC编译问题问询
Hey there! Since you already got the find_all example up and running via CMake, we can translate that setup into a straightforward GCC workflow for your custom project. Let’s break this down step by step:
1. Verify libFTDI Development Files Are Installed
First, make sure you have the libFTDI development package (which includes headers and shared libraries) ready. If you built libFTDI from source earlier, skip ahead—otherwise, install it via apt:
sudo apt-get update && sudo apt-get install libftdi1-dev
2. Use pkg-config to Simplify Compilation Flags
LibFTDI works seamlessly with pkg-config, which takes care of finding header paths and linking libraries automatically. Run these commands to confirm it’s configured correctly:
- Get compiler flags (to locate libFTDI headers):
You’ll see output likepkg-config --cflags libftdi1-I/usr/include/libftdi1if it’s working. - Get linker flags (to link against the libFTDI library):
Expect output likepkg-config --libs libftdi1-lftdi1(plus any dependent system libraries).
3. Write Your Custom Code
Let’s start with a simple test script (save as my_ftdi_scanner.c) that replicates the core functionality of find_all:
#include <stdio.h> #include <libftdi1/ftdi.h> int main() { struct ftdi_context ftdi; struct ftdi_device_list *devlist, *curdev; int ret, num_devices; // Initialize FTDI context if (ftdi_init(&ftdi) < 0) { fprintf(stderr, "Failed to initialize FTDI context\n"); return 1; } // Scan for all FTDI devices if ((ret = ftdi_usb_find_all(&ftdi, &devlist, 0, 0)) < 0) { fprintf(stderr, "Failed to find devices: %s\n", ftdi_get_error_string(&ftdi)); ftdi_deinit(&ftdi); return 1; } num_devices = ret; printf("Found %d FTDI devices:\n", num_devices); // Iterate through found devices and print details for (curdev = devlist; curdev != NULL; curdev = curdev->next) { char manufacturer[128], description[128], serial[128]; ret = ftdi_usb_get_strings(&ftdi, curdev->dev, manufacturer, 128, description, 128, serial, 128); if (ret < 0) { fprintf(stderr, "Failed to get device strings: %s\n", ftdi_get_error_string(&ftdi)); continue; } printf("- Manufacturer: %s\n Description: %s\n Serial: %s\n", manufacturer, description, serial); } // Clean up resources ftdi_list_free(&devlist); ftdi_deinit(&ftdi); return 0; }
4. Compile with GCC
Use the pkg-config flags directly in your GCC command to avoid manual path or library errors:
gcc my_ftdi_scanner.c -o my_ftdi_scanner $(pkg-config --cflags --libs libftdi1)
The $(...) syntax runs the pkg-config command and inserts its output into the GCC call—this ensures GCC knows exactly where to find libFTDI headers and which libraries to link against.
5. Run Your Program (Fix Permissions if Needed)
By default, non-root users might not have access to USB FTDI devices. You have two options:
- Run with sudo temporarily:
sudo ./my_ftdi_scanner - Set up permanent udev rules (better for daily use):
- Create a udev rule file:
sudo nano /etc/udev/rules.d/99-ftdi.rules - Add this line (covers most standard FTDI devices; adjust vendor/product IDs if you’re using a custom device):
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", MODE="0666" - Reload udev rules and apply changes:
sudo udevadm control --reload-rules && sudo udevadm trigger - Log out and back in, then run your program without sudo:
./my_ftdi_scanner
- Create a udev rule file:
Troubleshooting Common Issues
- "fatal error: libftdi1/ftdi.h: No such file or directory": Ensure
libftdi1-devis installed. If you built libFTDI from source, add-I/path/to/your/libftdi/includeto the compile command to point GCC to the headers. - **"undefined reference to
ftdi_init'"**: You forgot to link against libFTDI—double-check that you included$(pkg-config --libs libftdi1)` in your compile command.
内容的提问来源于stack exchange,提问作者b00leant

