Eclipse CDT调用MSYS2下载的OpenCV/Tesseract时出现未定义引用问题
Hey there! Let’s walk through troubleshooting that undefined reference error you’re facing when setting up OpenCV and Tesseract in Eclipse CDT with MSYS2 MinGW. These errors almost always boil down to missing library links or mismatched configurations, so let’s start with the most common fixes:
First, make sure you’ve installed the correct MinGW-specific packages for OpenCV and Tesseract—not generic or manually compiled versions. Open the MSYS2 terminal that matches your Eclipse setup (e.g., MSYS2 MinGW 64-bit for 64-bit projects) and run:
# For 64-bit systems pacman -S mingw-w64-x86_64-opencv mingw-w64-x86_64-tesseract # For 32-bit systems pacman -S mingw-w64-i686-opencv mingw-w64-i686-tesseract
Verify they’re installed properly with pacman -Qs opencv and pacman -Qs tesseract—you should see the packages listed under your MinGW prefix (like mingw64).
Undefined references usually mean the linker can’t find the library files. Let’s update your project settings:
- Right-click your project > Properties
- Go to C/C++ General > Paths and Symbols > Includes > GNU C++:
Add these paths (adjust for 32-bit by replacingmingw64withmingw32):C:\msys64\mingw64\includeC:\msys64\mingw64\include\opencv4(OpenCV 4 nests its headers here)
- Go to C/C++ General > Paths and Symbols > Libraries > GNU C++:
Add the core libraries you need (adjust based on your project’s features):- OpenCV:
opencv_core,opencv_imgproc,opencv_highgui - Tesseract:
tesseract,leptonica(Tesseract depends on Leptonica, so don’t skip this)
- OpenCV:
- Go to C/C++ Build > Settings > MinGW C++ Linker > Libraries:
Add the library path:C:\msys64\mingw64\lib(again, adjust for 32-bit)
This is a super easy mistake to make! Confirm:
- You used the right MSYS2 terminal when installing packages (64-bit terminal for 64-bit libraries, etc.)
- Your Eclipse toolchain matches. Go to Window > Preferences > C/C++ > Build > Toolchains and select the MinGW toolchain for your architecture.
- In project properties, check C/C++ Build > Settings > Tool Settings—the compiler/linker paths should point to the correct MinGW
binfolder (e.g.,C:\msys64\mingw64\bin).
Stale build files can sometimes cause weird undefined reference errors. Do this:
- Right-click your project > Clean
- Once clean finishes, right-click > Build Project
- If that doesn’t work, manually delete the
DebugorReleasefolders in your project directory, then rebuild from scratch.
Double-check you’re using the libraries correctly in your code:
- For OpenCV, include the main header with
#include <opencv2/opencv.hpp>and use thecv::namespace (or addusing namespace cv;for brevity). - For Tesseract, include
#include <tesseract/baseapi.h>and initialize the API properly, like:tesseract::TessBaseAPI api; if (api.Init(NULL, "eng") != 0) { // Handle initialization error here }
If you’re still hitting errors, share the exact undefined reference message (e.g., which function/symbol is missing)—that’ll help narrow down the issue even more!
内容的提问来源于stack exchange,提问作者Vaio

