C++ 如何使用 OpenCV 2.4.3 和 Code::Blocks 编译程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10876052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:34:13  来源:igfitidea点击:

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

c++opencvcodeblocks

提问by flowfree

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

我已经用 MinGW和 OpenCV 2.4.3安装了Code::Blocks。我想编译这个简单的程序:

#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?

如何正确设置 CodeBlocks 来编译上面的代码?

I have installed OpenCV in c:\opencv.

我已经在c:\opencv.

回答by flowfree

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

要将 OpenCV 与 CodeBlocks 一起使用,您需要告诉 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. 包含 OpenCV 头文件的目录 → c:\opencv\build\include
  2. 链接所需的 OpenCV 库列表 → c:\opencv\build\x86\mingw\libs\libopencv_*.dll.a


1. Add the OpenCV header files directory

1.添加OpenCV头文件目录

Open SettingsCompiler and debugger...Search directoriestab → Compilertab

打开设置编译器和调试器...搜索目录选项卡 →编译器选项卡

CodeBlock settings: search directory

CodeBlock 设置:搜索目录

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

单击添加按钮以添加新条目。在弹出的对话框中,输入c:\opencv\build\include

Add OpenCV include directory to CodeBlocks

将 OpenCV 包含目录添加到 CodeBlocks

and click Ok.

并单击确定



2. Add the OpenCV libraries needed for linking

2.添加链接所需的OpenCV库

Open SettingsCompiler and debugger... → Linker settingstab.

打开设置编译器和调试器... →链接器设置选项卡。

CodeBlocks linker settings

CodeBlocks 链接器设置

Click addfor adding new entries and open a popup dialog.

单击添加以添加新条目并打开一个弹出对话框。

CodeBlock settings: Add libraries to link

CodeBlock 设置:添加库链接

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

单击“...”按钮打开文件打开对话框。转到c:\opencv\build\x86\mingw\lib目录并按 Ctrl-A 选择所有文件。

enter image description here

在此处输入图片说明

Click Opento add the files,

单击打开以添加文件,

enter image description here

在此处输入图片说明

Click Okto 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.

现在您已经设置了包含目录和用于链接的库,您可以通过按 F9 键来编译和运行您的项目。

回答by Iván Qui?ones

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).

我遇到了同样的问题,最后我不得不用 MINGW 卸载代码块,然后安装 JUST MINGW(使用http://sourceforge.net/projects/mingw/files/Installer/ 中的mingw-get ),然后安装代码没有 MINGW 的块(看起来与代码块绑定的 mingw 版本可能与 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 7by @bsdnoobz

如果在那之后并检查您的路径和代码块编译器和链接器配置,您的程序编译但崩溃并出现 000005 错误,这意味着您的预编译 opencv 文件有一些未知问题(因为一些小精灵搞乱了它们)因此如果发生此错误你,你需要使用 NENAD BULATOVIC 对这个问题的回答来自己编译 opencv开始使用 OpenCV 2.4 和 Windows 7 上MinGW由@bsdnoobz