9

I've installed Code::Blocks with MinGW and OpenCV 2.4.3. I want to compile this simple program:

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

using namespace cv;
using namespace std;

int main()
{
  Mat im = imread("c:/path/to/lena.jpg");
  if (im.empty()) {
    cout << "Cannot open image!" << endl;
    return 1;
  }
  imshow("Image", im);
  waitKey(0);
}

How to properly setup CodeBlocks for compiling the code above?

I have installed OpenCV in c:\opencv.

flowfree
  • 16,356
  • 12
  • 52
  • 76
  • Is it the same for x64??? I have errors http://stackoverflow.com/questions/11107022/installing-opencv2-4-1-on-windows-7-x64-mingw-codeblocks – rgap Jun 19 '12 at 23:19

2 Answers2

15

To use OpenCV with CodeBlocks, you need to tell CodeBlocks:

  1. The directory which contains the OpenCV header files → c:\opencv\build\include
  2. List of OpenCV libraries needed for linking → c:\opencv\build\x86\mingw\libs\libopencv_*.dll.a

1. Add the OpenCV header files directory

Open SettingsCompiler and debugger...Search directories tab → Compiler tab

CodeBlock settings: search directory

Click add button for adding a new entry. In the popup dialog, type c:\opencv\build\include,

Add OpenCV include directory to CodeBlocks

and click Ok.


2. Add the OpenCV libraries needed for linking

Open SettingsCompiler and debugger... → Linker settings tab.

CodeBlocks linker settings

Click add for adding new entries and open a popup dialog.

CodeBlock settings: Add libraries to link

Click the "..." button to open the File Open Dialog. Go to c:\opencv\build\x86\mingw\lib directory and select all files by pressing Ctrl-A.

enter image description here

Click Open to add the files,

enter image description here

Click Ok to save all settings.


Now that you've set the include directory and the libraries for linking, you can compile and run your project by pressing F9 key.

flowfree
  • 16,356
  • 12
  • 52
  • 76
  • It shows libopencv_core240.dll missing from computer. What to do? – Abid Rahman K Jun 05 '12 at 17:42
  • 2
    @AbidRahmanK Have you installed OpenCV and add `c:\opencv\build\x86\mingw\bin` to system PATH? See http://stackoverflow.com/q/10860352/1396314 – flowfree Jun 05 '12 at 18:23
  • 1
    I tried both ,with codeblock and vc++. both same error. I tried it exactly you have written. – Abid Rahman K Jun 05 '12 at 18:25
  • Two of the links for the image is broken. could you please fix them? – Yuchen May 21 '14 at 18:59
  • It am getting `undefined references` errors after following all these steps – mr5 Nov 14 '16 at 08:00
  • @AbidRahmanK In 2012 you were struggling while installing OpenCV and now you are one of the genii of OpenCV _/\\_ – udit043 Dec 14 '17 at 14:46
  • @udit043: back then I struggled a little bit setting up opencv in windows. In Linux, it was far easier to get started. – Abid Rahman K Dec 15 '17 at 15:17
  • @AbidRahmanK Year before, I have successfully installed OpenCV in Windows 7 and worked on some projects (https://youtu.be/i4HqZAH_TZU) but currently struggling in installing in Windows 10 so installing Ubuntu using VMware :) – udit043 Dec 15 '17 at 16:08
0

I had those same problems, in the end i had to UNINSTALL CODEBLOCKS WITH MINGW, then install JUST MINGW (using mingw-get from here http://sourceforge.net/projects/mingw/files/Installer/), after that install code blocks WITHOUT MINGW (it looks like the mingw version boundled with codeblocks might have some compatibility issues with the precompiled files of opencv).

IF after that and reviewing your path and codeblocks compiler and linker config, your program compiles but crashes with a 000005 error, it means that your precompiled opencv files have some unknown problem (because some little elfs messed with them) therefore if this error occurs to you, you will need to compile opencv by yourself using the answer of NENAD BULATOVIC to this question Getting started with OpenCV 2.4 and MinGW on Windows 7 by @bsdnoobz

Community
  • 1
  • 1
Iván Quiñones
  • 501
  • 4
  • 12