Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

c++ - How to compile a program using OpenCV 2.4.3 with Code::Blocks?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

  1. The directory which contains the OpenCV header files → c:opencvuildinclude
  2. List of OpenCV libraries needed for linking → c:opencvuildx86mingwlibslibopencv_*.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:opencvuildinclude,

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:opencvuildx86mingwlib 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...