You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Raspbian(树莓派3)下libFTDI自定义项目GCC编译问题问询

Building Custom libFTDI Projects with GCC on Raspbian (Raspberry Pi 3)

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):
    pkg-config --cflags libftdi1
    
    You’ll see output like -I/usr/include/libftdi1 if it’s working.
  • Get linker flags (to link against the libFTDI library):
    pkg-config --libs libftdi1
    
    Expect output like -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):
    1. Create a udev rule file:
      sudo nano /etc/udev/rules.d/99-ftdi.rules
      
    2. 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"
      
    3. Reload udev rules and apply changes:
      sudo udevadm control --reload-rules && sudo udevadm trigger
      
    4. Log out and back in, then run your program without sudo:
      ./my_ftdi_scanner
      

Troubleshooting Common Issues

  • "fatal error: libftdi1/ftdi.h: No such file or directory": Ensure libftdi1-dev is installed. If you built libFTDI from source, add -I/path/to/your/libftdi/include to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:03:55