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

在Fedora上用Eclipse为树莓派2B交叉编译OpenCV的技术求助

Getting OpenCV Working with Eclipse Cross-Compilation for Raspberry Pi 2 on Fedora

Hey there! I totally get where you're coming from—most guides do lean towards Ubuntu or CMake, but setting this up on Fedora with Eclipse is absolutely doable. Let's break this down step by step.

1. First: Cross-Compile OpenCV for Raspberry Pi 2

Since your Fedora system runs on x86, you can't use the regular OpenCV packages—you need an ARM-compiled version tailored for the Pi 2. Here's how to build it:

  • Install the cross-compilation toolchain on Fedora:
    sudo dnf install arm-linux-gnueabihf-gcc-c++
    
  • Grab the OpenCV source code (I recommend using a stable release like 4.x):
    git clone https://github.com/opencv/opencv.git
    cd opencv
    git checkout 4.5.5  # Or your preferred stable version
    mkdir build && cd build
    
  • Configure CMake for cross-compilation:
    Use OpenCV's built-in ARM toolchain file, and set options optimized for the Pi 2 (which supports NEON and VFPV3):
    cmake -DCMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabihf.toolchain.cmake \
          -DCMAKE_INSTALL_PREFIX=/home/your-user/rpi-opencv \  # Choose a path to install the compiled OpenCV
          -DENABLE_NEON=ON \
          -DENABLE_VFPV3=ON \
          -DBUILD_opencv_java=OFF \
          -DBUILD_opencv_python=OFF \
          -DBUILD_EXAMPLES=OFF \
          ..
    
  • Build and install:
    make -j$(nproc)  # Use all your CPU cores for faster compilation
    sudo make install
    

This will give you a fully ARM-compatible OpenCV installation in your chosen prefix.

2. Configure Eclipse to Use the Cross-Compiled OpenCV

Now that you have the ARM OpenCV binaries, you need to tell your Eclipse project where to find them:

A. Update Build Settings

  1. Right-click your Eclipse project → Properties
  2. Go to C/C++ Build → Settings
    • GCC Cross Compiler → Includes:
      Click "Add" and add the include directories from your cross-compiled OpenCV:
      • /home/your-user/rpi-opencv/include
      • /home/your-user/rpi-opencv/include/opencv4 (for OpenCV 4.x, since headers are nested here)
    • GCC Cross Linker → Libraries:
      • Under "Libraries (-l)", add the OpenCV libraries you need (e.g., opencv_core, opencv_imgproc, opencv_highgui)
      • Under "Library search path (-L)", add /home/your-user/rpi-opencv/lib

B. Fix Code Editor Paths (Avoid Red Underlines)

  1. Still in Properties, go to C/C++ General → Paths and Symbols
  2. Includes → GNU C++:
    Add the same include directories as above so Eclipse's editor can find the OpenCV headers.
  3. Library Paths:
    Add /home/your-user/rpi-opencv/lib here too.

Optional: Use pkg-config for Easier Configuration

If you prefer, you can use pkg-config to auto-include flags instead of manual paths:

  • In C/C++ Build → Settings → GCC Cross Compiler → Miscellaneous, add:
    $(pkg-config --cflags opencv4)
    
  • In GCC Cross Linker → Miscellaneous, add:
    $(pkg-config --libs opencv4)
    
  • Then set the PKG_CONFIG_PATH environment variable in Eclipse: Go to Run → Run Configurations → Environment, add a variable named PKG_CONFIG_PATH with value /home/your-user/rpi-opencv/lib/pkgconfig

3. Test Your Setup

Write a simple test program to verify everything works:

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

int main() {
    // Create a blank image
    cv::Mat img = cv::Mat::zeros(200, 200, CV_8UC3);
    // Draw some text
    cv::putText(img, "Hello RPi OpenCV!", cv::Point(10, 100), 
                cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0));
    // Save the image
    cv::imwrite("test.jpg", img);
    std::cout << "Image saved successfully!" << std::endl;
    return 0;
}

Compile the project, then transfer the executable to your Raspberry Pi (use scp or a USB drive). On the Pi, run it with:

./your-program-name

If you see the success message and a test.jpg file, you're good to go!

Troubleshooting Tips

  • Missing headers: Double-check your include paths—OpenCV 4.x nests headers under opencv4/opencv2/, so make sure you've added both the top-level include dir and the opencv4 subdir.
  • Linker errors: Ensure you're linking all the necessary OpenCV libraries, and that your library path points to the ARM-compiled libs (not your system's x86 OpenCV).
  • Pi runtime errors: If the program fails to run on the Pi, make sure the Pi has access to the OpenCV libraries. You can either copy the .so files from /home/your-user/rpi-opencv/lib to the Pi's /usr/lib/arm-linux-gnueabihf/, or set LD_LIBRARY_PATH when running:
    LD_LIBRARY_PATH=/path/to/copied/libs ./your-program-name
    

内容的提问来源于stack exchange,提问作者mat cape

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:27:34