Search code examples
c++opencveclipse-cdttesseractmsys2

Eclipse CDT undefined reference (package that was downloaded via MYSYS2)


So I basically only coded in Java at College, this time I want to start my new Project in C++ and want to keep the Eclipse IDE that I have always used. I need the openCV and Tesseract packages (import). I have googled and researched for quite a time and I seem to be doing it right? but maybe some of you can tell me otherwise.

What I did:

  1. Downloaded Eclipse CDT
  2. Downloaded MYSYS2
  3. Followed this instructions (MinGW Compiler)

    1. Open MSYS2 shell from start menu
    2. Run pacman -Sy pacman to update the package database
    3. Re-open the shell, run pacman -Syu to update the package database and core system packages
    4. Re-open the shell, run pacman -Su to update the rest
    5. (Reference)
      • For 64 bits, run pacman -S mingw-w64-x86_64-toolchain
    6. Select which package to install, default is all
    7. You may also need make, run pacman -S make
  4. Installed the libraries/tools that i need

    OpenCV

    pacman -S mingw64/mingw-w64-x86_64-opencv

    Tesseract

    pacman -S mingw64/mingw-w64-x86_64-tesseract-ocr

  5. Included the MinGW to PATH (system environment variables)->restart computer

  6. Started a new Eclipse Project-> C++ -> choose MinGW GCC as Toolchain

  7. Basic Hello World -> Works fine

  8. Basic OpenCV example -> doesnt work

It seems to Resolve the Inclusions correctly.

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

No Errors there.

FullCode:

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

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

What Eclipse says:

16:54:43 **** Incremental Build of configuration Debug for project hello ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\hello.o" "..\\src\\hello.cpp" 
g++ -o hello.exe "src\\hello.o" 
src\hello.o: In function `main':
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:17: undefined reference to `cv::imread(cv::String const&, int)'
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:25: undefined reference to `cv::namedWindow(cv::String const&, int)'
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:26: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:28: undefined reference to `cv::waitKey(int)'
src\hello.o: In function `cv::String::String(char const*)':
C:/msys64/mingw64/include/opencv2/core/cvstd.hpp:602: undefined reference to `cv::String::allocate(unsigned long long)'
src\hello.o: In function `cv::String::~String()':
C:/msys64/mingw64/include/opencv2/core/cvstd.hpp:648: undefined reference to `cv::String::deallocate()'
src\hello.o: In function `cv::Mat::~Mat()':
C:/msys64/mingw64/include/opencv2/core/mat.inl.hpp:692: undefined reference to `cv::fastFree(void*)'
src\hello.o: In function `cv::Mat::release()':
C:/msys64/mingw64/include/opencv2/core/mat.inl.hpp:804: undefined reference to `cv::Mat::deallocate()'
src\hello.o: In function `cv::Mat::operator=(cv::Mat&&)':
C:/msys64/mingw64/include/opencv2/core/mat.inl.hpp:1371: undefined reference to `cv::fastFree(void*)'
collect2.exe: error: ld returned 1 exit status

16:54:46 Build Finished (took 2s.908ms)

It can't find the librarys???? Whats the point of downloading it through MSYS2 if it doesnt connect the library like it does with iostream

Do I need to add all the library objects to the linker settings C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries


Solution

  • Okay so I guess that its obv but for those who have the same mistake, MSYS2 just added all opencv and tesseract files.

    If you want to use them you need to specify it to the Linker.

    filename: libopencv_core.dll.a

    you need to exclude the lib at the beginning and the .dll and .a

    linker: opencv_core

    all libs can be found on the mingw path: (C:\msys64\mingw64\lib)

    in the end you link it with -lopencv_core

    or through the Eclipse GUI C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries -> add library -> opencv_core