Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling openCV 2.3.1 programs with MinGW gcc/g++ on Windows 7 64bit

For a week I've been struggling with compiling openCV programs. I've tried everything I could possibly find on the internet.

What I did is: I've downloaded OpenCV-2.3.1-win-superpack.exe and followed this official installation guide.

In the CMake (gui) my source was: D:\opencv and build destination was: C:\opencv.

I've also added C:\opencv\install\bin;C:\opencv\bin to my system's PATH variable.

What I want is to compile openCV programs on my Windows OS using MinGW's gcc/g++ compilers.

I've tried various gcc/g++ parameters that I've found on the internet and days playing with the -I and -L options the compiler can never find the openCV functions or structures.

What I am trying to compile:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

int main(int argc, char *argv[])
{
  // Nothing but create a window
  cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 
  cvMoveWindow("mainWin", 100, 100);
  cvWaitKey(0);
  return 0;
}

Error:

Input:

gcc test.c -o test -I"C:\opencv\install\include" -I"C:\opencv\install\include\opencv" -L"C:\opencv\install\bin"

Output:

...\ccK4MfHv.o:test.c:(.text+0xa0b): undefined reference to `cvFree_'

Or with g++:

Input:

g++ test.c -o test -I"C:\opencv\install\include" -I"C:\opencv\install\include\opencv" -L"C:\opencv\install\bin"

Output:

...\ccXCTKa1.o:test.c:(.text+0x1e): undefined reference to `cvNamedWindow'

Side note: trying to compile in VS2005 I get the same error.

Thank you for your time!

like image 593
Kirill.lv Avatar asked Mar 19 '12 23:03

Kirill.lv


1 Answers

In case someone else needs to solve this issue, here's how I got the posted OpenCV/HighGUI sample code to compile in Windows 7 x64 using MinGW, MSYS, and CMake:

  1. build OpenCV from source using MinGW/MSYS/CMake. This is because I could not get the MinGW compiled version in the OpenCV-win-SuperPack to link properly in MinGW/MSYS/Windows 7 x64.

    For full reference, here's how I compiled OpenCV:

    • make sure you have an up-to-date CMake (v2.6 or later) and MinGW (with GCC, G++, and MSYS options) installed
    • if you want the new Qt-based OpenCV HighGUI front-end, you will need to install Qt 4 (SDK).
    • download a OpenCV source/superpack version 2.2 or later (I used OpenCV-2.3.1-win-superpack.exe)
    • unzip the contents to [OPENCV_SOURCE_DIR] (I put it in C:/opencv, so there should be a file at C:/opencv/README for example)
    • create a [OPENCV_BUILD_DIR] directory elsewhere (I used C:/opencv/build/mingw)
    • use the CMake-GUI tool, specify the source directory as [OPENCV_SOURCE_DIR], the build directory as [OPENCV_BUILD_DIR], and click "Configure".
    • you may wish/need to go tweak the options (e.g. I ticked "Qt" and "Qt-OpenGL" entries, then clicked "Configure" again, then had to provide the path to the qmake executable)
    • once you have finished configuring OpenCV, click "Generate"
    • in a MSYS terminal, browse to [OPENCV_BUILD_DIR], and run "make" to build the code (this may take a while)
    • once the has been built properly, run "make install", which collects the built code/libraries/include dirs into [OPENCV_BUILD_DIR]/install folder (or a different folder if you changed the corresponding option when using the CMake-GUI tool)
    • add [OPENCV_BUILD_DIR]/install/bin folder to the PATH environmental variable. If you do not know how to do this, then I'd recommend using the Path Editor GUI tool.
    • if you end up using Qt, you will also need to put the bin folder of Qt SDK in the PATH environmental variable. This is the folder that includes qmake.exe.
  2. put the following sample code into a file called test.c. I modified the includes slightly to make them compatible with OpenCV v2.2 and above.

     #include <stdlib.h>
     #include <stdio.h>
     #include <math.h>
     #include <opencv/cv.h>
     #include <opencv/highgui.h>
    
     int main(int argc, char *argv[])
     {
       // Nothing but create a window
       cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); 
       cvMoveWindow("mainWin", 100, 100);
       cvWaitKey(0);
       return 0;
     } 
    
  3. in a MSYS terminal, browse to the folder where you put test.c, and run:

    gcc -o test -I"[OPENCV_BUILD_DIR]/install/include" test.c \
      -L"[OPENCV_BUILD_DIR]/install/lib" \
      -lopencv_core[OPENCV_VERSION] \
      -lopencv_imgproc[OPENCV_VERSION] \
      -lopencv_highgui[OPENCV_VERSION]
    

    So in my case:

    gcc -o test -I"/c/opencv/build/mingw/install/include" test.c \
      -L"/c/opencv/build/mingw/install/lib" \
      -lopencv_core231
      -lopencv_imgproc231
      -lopencv_highgui231
    

Path Editor: http://www.redfernplace.com/software-projects/patheditor/

like image 99
Anqi Avatar answered Nov 02 '22 18:11

Anqi