Reputation: 16462
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
.
Upvotes: 9
Views: 32494
Reputation: 521
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
Upvotes: 0
Reputation: 16462
To use OpenCV with CodeBlocks, you need to tell CodeBlocks:
c:\opencv\build\include
c:\opencv\build\x86\mingw\libs\libopencv_*.dll.a
1. Add the OpenCV header files directory
Open Settings → Compiler and debugger... → Search directories tab → Compiler tab
Click add button for adding a new entry. In the popup dialog, type c:\opencv\build\include
,
and click Ok.
2. Add the OpenCV libraries needed for linking
Open Settings → Compiler and debugger... → Linker settings tab.
Click add for adding new entries and open a popup dialog.
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.
Click Open to add the files,
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.
Upvotes: 15