在Fedora上用Eclipse为树莓派2B交叉编译OpenCV的技术求助
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
- Right-click your Eclipse project → Properties
- 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
- Under "Libraries (-l)", add the OpenCV libraries you need (e.g.,
- GCC Cross Compiler → Includes:
B. Fix Code Editor Paths (Avoid Red Underlines)
- Still in Properties, go to C/C++ General → Paths and Symbols
- Includes → GNU C++:
Add the same include directories as above so Eclipse's editor can find the OpenCV headers. - Library Paths:
Add/home/your-user/rpi-opencv/libhere 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_PATHenvironment variable in Eclipse: Go to Run → Run Configurations → Environment, add a variable namedPKG_CONFIG_PATHwith 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 theopencv4subdir. - 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
.sofiles from/home/your-user/rpi-opencv/libto the Pi's/usr/lib/arm-linux-gnueabihf/, or setLD_LIBRARY_PATHwhen running:LD_LIBRARY_PATH=/path/to/copied/libs ./your-program-name
内容的提问来源于stack exchange,提问作者mat cape

