OpenCV 2.3 doesn't compile, undefined reference error

13,278

Solution 1

Are you using the precompiled MinGW libraries? (it appears so). I tried for a couple of days to get my project to link successfully under Code::Blocks. I was seeing the same sort of errors you describe (bad references, etc).

I finally recompiled OpenCV using the steps shown here, and all became well.

Solution 2

I guess the Problem is that the library path points to the vc10 directory (-L/cygdrive/C/OpenCV2.3/build/x86/vc10/lib) instead of the mingw directory (-L/cygdrive/C/OpenCV2.3/build/x86/mingw/lib). The ld-linker can't find the methods because they are mangled for the vc++ "link.exe".

If you use 64 bit, then you have also link against 64-bit libs: -L/cygdrive/C/OpenCV2.3/build/x64/mingw/lib.

Share:
13,278
Wiliam
Author by

Wiliam

My cat is fat

Updated on June 04, 2022

Comments

  • Wiliam
    Wiliam almost 2 years

    I'm using OpenCV in Windows 7 64bits and Netbeans 7.0. I tried to compile the next code using MinGW and cygwin but both fails with undefined references.

    When I use MAT or FLANN and others I can't compile, but I'm adding all libraries (I tried only adding Debug ones, Release ones, only needed ones... but fails).

    The same code in ubuntu works, but I need to compile it in windows too. I'm using the 2.3 compiled version (using CMake) and the installable one.

    #include "opencv2\opencv.hpp"
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
        cv::Mat::eye(1, 1, 0);
    
        return 0;
    }
    

    "/usr/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .clean-conf
    make[1]: Entering directory `/cygdrive/f/Proyectos/C++/OpenCV23Sandbox'
    rm -f -r build/Release
    rm -f dist/Release/Cygwin-Windows/opencv23sandbox.exe
    make[1]: Leaving directory `/cygdrive/f/Proyectos/C++/OpenCV23Sandbox'
    
    CLEAN SUCCESSFUL (total time: 1s)
    "/usr/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .build-conf
    make[1]: Entering directory `/cygdrive/f/Proyectos/C++/OpenCV23Sandbox'
    "/usr/bin/make"  -f nbproject/Makefile-Release.mk dist/Release/Cygwin-Windows/opencv23sandbox.exe
    make[2]: Entering directory `/cygdrive/f/Proyectos/C++/OpenCV23Sandbox'
    mkdir -p build/Release/Cygwin-Windows
    rm -f build/Release/Cygwin-Windows/main.o.d
    g++.exe    -c -O2 -I/cygdrive/C/OpenCV2.3/build/include -MMD -MP -MF build/Release/Cygwin-Windows/main.o.d -o build/Release/Cygwin-Windows/main.o main.cpp
    mkdir -p dist/Release/Cygwin-Windows
    g++.exe     -o dist/Release/Cygwin-Windows/opencv23sandbox build/Release/Cygwin-Windows/main.o -L/cygdrive/C/OpenCV2.3/build/x86/vc10/lib -lopencv_calib3d230 -lopencv_calib3d230d -lopencv_contrib230 -lopencv_contrib230d -lopencv_core230 -lopencv_core230d -lopencv_features2d230 -lopencv_features2d230d -lopencv_flann230 -lopencv_flann230d -lopencv_gpu230 -lopencv_gpu230d -lopencv_haartraining_engine -lopencv_haartraining_engined -lopencv_highgui230 -lopencv_highgui230d -lopencv_imgproc230 -lopencv_imgproc230d -lopencv_legacy230 -lopencv_legacy230d -lopencv_ml230 -lopencv_ml230d -lopencv_objdetect230 -lopencv_objdetect230d -lopencv_video230 -lopencv_video230d 
    build/Release/Cygwin-Windows/main.o:main.cpp:(.text+0xac): undefined reference to `cv::Mat::eye(int, int, int)'
    build/Release/Cygwin-Windows/main.o:main.cpp:(.text+0x106): undefined reference to `cv::fastFree(void*)'
    build/Release/Cygwin-Windows/main.o:main.cpp:(.text+0x16f): undefined reference to `cv::fastFree(void*)'
    build/Release/Cygwin-Windows/main.o:main.cpp:(.text+0x1dd): undefined reference to `cv::fastFree(void*)'
    build/Release/Cygwin-Windows/main.o:main.cpp:(.text+0x1fa): undefined reference to `cv::Mat::deallocate()'
    build/Release/Cygwin-Windows/main.o:main.cpp:(.text+0x20a): undefined reference to `cv::Mat::deallocate()'
    build/Release/Cygwin-Windows/main.o:main.cpp:(.text+0x21a): undefined reference to `cv::Mat::deallocate()'
    build/Release/Cygwin-Windows/main.o:main.cpp:(.text$_ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x66): undefined reference to `cv::Mat::deallocate()'
    build/Release/Cygwin-Windows/main.o:main.cpp:(.text$_ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x5e): undefined reference to `cv::fastFree(void*)'
    collect2: ld returned 1make[2]: Leaving directory `/cygdrive/f/Proyectos/C++/OpenCV23Sandbox'
    make[1]: Leaving directory `/cygdrive/f/Proyectos/C++/OpenCV23Sandbox'
     exit status
    make[2]: *** [dist/Release/Cygwin-Windows/opencv23sandbox.exe] Error 1
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2
    
    BUILD FAILED (exit value 2, total time: 2s)
    

    Trying with MinGW libs:

    "/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .clean-conf
    make[1]: Entering directory `/f/Proyectos/C++/OpenCV23Sandbox'
    rm -f -r build/Release
    rm -f dist/Release/MinGW-Windows/opencv23sandbox.exe
    make[1]: Leaving directory `/f/Proyectos/C++/OpenCV23Sandbox'
    
    CLEAN SUCCESSFUL (total time: 350ms)
    "/bin/make" -f nbproject/Makefile-Release.mk QMAKE= SUBPROJECTS= .build-conf
    make[1]: Entering directory `/f/Proyectos/C++/OpenCV23Sandbox'
    "/bin/make"  -f nbproject/Makefile-Release.mk dist/Release/MinGW-Windows/opencv23sandbox.exe
    make[2]: Entering directory `/f/Proyectos/C++/OpenCV23Sandbox'
    mkdir -p build/Release/MinGW-Windows
    rm -f build/Release/MinGW-Windows/main.o.d
    g++.exe    -c -O2 -I/C/OpenCV2.3/build/include -MMD -MP -MF build/Release/MinGW-Windows/main.o.d -o build/Release/MinGW-Windows/main.o main.cpp
    mkdir -p dist/Release/MinGW-Windows
    g++.exe     -o dist/Release/MinGW-Windows/opencv23sandbox build/Release/MinGW-Windows/main.o -L../OpenCV/OpenCV2.3/build/x86/mingw/lib ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_calib3d230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_contrib230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_core230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_features2d230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_flann230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_gpu230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_highgui230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_imgproc230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_legacy230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_ml230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_objdetect230.dll.a ../OpenCV/OpenCV2.3/build/x86/mingw/lib/libopencv_video230.dll.a 
    build/Release/MinGW-Windows/main.o:main.cpp:(.text+0x4f): undefined reference to `cv::Mat::eye(int, int, int)'
    build/Release/MinGW-Windows/main.o:main.cpp:(.text+0xac): undefined reference to `cv::fastFree(void*)'
    build/Release/MinGW-Windows/main.o:main.cpp:(.text+0x11a): undefined reference to `cv::fastFree(void*)'
    build/Release/MinGW-Windows/main.o:main.cpp:(.text+0x191): undefined reference to `cv::fastFree(void*)'
    build/Release/MinGW-Windows/main.o:main.cpp:(.text+0x1a7): undefined reference to `cv::Mat::deallocate()'
    build/Release/MinGW-Windows/main.o:main.cpp:(.text+0x1ba): undefined reference to `cv::Mat::deallocate()'
    build/Release/MinGW-Windows/main.o:main.cpp:(.text+0x1ce): undefined reference to `cv::Mat::deallocate()'
    build/Release/MinGW-Windows/main.o:main.cpp:(.text$_ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x74): undefined reference to `cv::Mat::deallocate()'
    build/Release/MinGW-Windows/main.o:main.cpp:(.text$_ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x63): undefined reference to `cv::fastFree(void*)'
    collect2: ld returned 1 exit status
    make[2]: Leaving directory `/f/Proyectos/C++/OpenCV23Sandbox'
    make[1]: Leaving directory `/f/Proyectos/C++/OpenCV23Sandbox'
    make[2]: *** [dist/Release/MinGW-Windows/opencv23sandbox.exe] Error 1
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2
    
    BUILD FAILED (exit value 2, total time: 1s)
    

    or

    g++.exe     -o dist/Release/MinGW-Windows/opencv23sandbox build/Release/MinGW-Windows/main.o -L../OpenCV/OpenCV2.3/build/x86/mingw/lib -lopencv_calib3d230.dll -lopencv_contrib230.dll -lopencv_core230.dll -lopencv_features2d230.dll -lopencv_flann230.dll -lopencv_gpu230.dll -lopencv_highgui230.dll -lopencv_imgproc230.dll -lopencv_legacy230.dll -lopencv_ml230.dll -lopencv_objdetect230.dll -lopencv_video230.dll 
    

    same result.

  • Wiliam
    Wiliam almost 13 years
    Uf... I'm in windows 7, I tried that before and all works except things like cv::Mat or cv:flann, I'm adding that libreries... Well, I'm going to try to compile them with MinGW and not VS10.
  • vstm
    vstm almost 13 years
    Youre using 64 bit windows? Then you have to link against the 64-bit builds: -L/cygdrive/C/OpenCV2.3/build/x64/mingw/lib.
  • Wiliam
    Wiliam almost 13 years
    Using 64 bits for the libs is only for optimizing, anyways I tried it and same error. Thanks again.