如何在 Windows 上正确地将 OpenCV 库链接到 Eclipse?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37238431/
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
How can I correctly link OpenCV libraries to Eclipse on Windows?
提问by JimmyJumbo
For the last couple of hours I have been trying to get a basic OpenCV program to work in my Eclipse Mars IDE. The program consists of the following main.cpp:
在过去的几个小时里,我一直试图让一个基本的 OpenCV 程序在我的 Eclipse Mars IDE 中工作。该程序由以下 main.cpp 组成:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
What I have done up till now:
到目前为止我做了什么:
- Installed MinGW (in E:\NVPACK\MinGW) and added its bin file path to my Environmental Variables.
Installed OpenCV 2.4.12 at E:\opencv. Inside the installed folder are the "build" and "sources" folder.
Made a new Eclipse C++ project with the MinGW GCC Toolchain.
Inside the Tool Settings (Project Properties --> C/C++ Build --> Settings) I included the OpenCV library in the GCC C++ Compiler as "E:\opencv\build\include"
Inside the Tool Settings I added in the MinGW C++ Linker all the libraries (i.e. opencv_core2412, opencv_highgui2412, opencv_imgproc2412) and set the Library search path to "E:\opencv\build\x86\vc12\lib"
Set my Binary parser to PE Windows Parser.
- 安装 MinGW(在 E:\NVPACK\MinGW 中)并将其 bin 文件路径添加到我的环境变量中。
在 E:\opencv 安装 OpenCV 2.4.12。在安装的文件夹中是“build”和“sources”文件夹。
使用 MinGW GCC 工具链创建了一个新的 Eclipse C++ 项目。
在工具设置(项目属性 --> C/C++ 构建 --> 设置)中,我将 OpenCV 库包含在 GCC C++ 编译器中作为“E:\opencv\build\include”
在工具设置中,我在 MinGW C++ 链接器中添加了所有库(即 opencv_core2412、opencv_highgui2412、opencv_imgproc2412)并将库搜索路径设置为“E:\opencv\build\x86\vc12\lib”
将我的二进制解析器设置为 PE Windows Parser。
Building the project all the OpenCV functions get undefined references errors, such as:
构建项目所有OpenCV函数都得到未定义引用错误,例如:
undefined reference to `cv::imread(std::string const&, int)' main.cpp line 17 C/C++ Problem
undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)' main.cpp line 26 C/C++ Problem
Weird enough, declaring Mat image does not give an error and holding my mouse over it correctly displays the OpenCV's Mat documentation.
奇怪的是,声明 Mat 图像不会出错,将鼠标悬停在它上面会正确显示 OpenCV 的 Mat 文档。
Googling the problem I found that for some reason I might need to use CMake to build my own library files (the ones used in step 5). So after installing CMake gui, I used E:\opencv\sources as the source directory and made a new directory E:\opencv\MinGW as my build directory. I used "Eclipse CDT4 -MinGW Makefiles" generator to Configure the files. After pressing Configure, I will receive the following error for my g++ compiler (a similar one for my gcc compiler):
谷歌搜索这个问题我发现由于某种原因我可能需要使用 CMake 来构建我自己的库文件(步骤 5 中使用的那些)。所以在安装CMake gui之后,我使用E:\opencv\sources作为源目录,并创建了一个新目录E:\opencv\MinGW作为我的构建目录。我使用“Eclipse CDT4 -MinGW Makefiles”生成器来配置文件。按配置后,我的 g++ 编译器会收到以下错误(我的 gcc 编译器类似):
CMake Error at CMakeLists.txt:71 (project):
The CMAKE_CXX_COMPILER:
E:/NVPACK/MinGW/bin/mingw32-g++
is not a full path to an existing compiler tool.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
The compilers are however identified: "The CXX compiler identification is GNU 4.8.1" and "The C compiler identification is GNU 4.8.1"
然而,编译器被标识:“CXX 编译器标识是 GNU 4.8.1”和“C 编译器标识是 GNU 4.8.1”
Any help on how to correctly set up the OpenCV 2.4.12 libraries in Eclipse Mars on Windows 7 would be very much appreciated!
非常感谢有关如何在 Windows 7 上的 Eclipse Mars 中正确设置 OpenCV 2.4.12 库的任何帮助!
回答by Vtik
Well, I hope this is detailed enough ! First of all, a big cup of coffee will help !
好吧,我希望这足够详细!首先,一大杯咖啡会有帮助!
Let's get started :
让我们开始吧 :
Requirements :
要求 :
- Download OpenCV source from github repo(extract it to a folder we will name
opencv_src_folder
) - Download and install MinGW (also add /bin folder to system path)
- Download and install CMake
- 从github repo下载 OpenCV 源代码(将其解压缩到我们将命名的文件夹中
opencv_src_folder
) - 下载并安装 MinGW(同时将 /bin 文件夹添加到系统路径)
- 下载并安装CMake
Steps :
脚步 :
Open CMake-gui
- set src folder as
opencv_src_folder
- set build folder as
opencv_src_folder/Mingw_build/
- Hit
configure
and chooseEclipse CDT4 - MinGW MakeFile
then next. - if configure successful , hit
generate
.
- set src folder as
Open a command line (Windows + R)
- $
cd opencv_src_folder/Mingw_build/
- $
mingw32-make
: this will make openCV using MinGW, so wait until it's finished ! - now, let's install it $
mingw32-make install
this will install cmake in opencv_src_folder/Mingw_build/install/
- $
Add
opencv_src_folder/Mingw_build/install/bin/
to your system path & restart session.Eclipse Configuration :
- Go to the Eclipse CDT IDE, create a C++ program using the sample OpenCV code.
- Go to Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes, and add the source OpenCV folder "opencv_src_folder/Mingw_build/install/include/"
Go to Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Libraries, and add to the Libraries (-l) separately : (Don't forget to change the 245 to whichever number they use for the lib files) opencv_calib3d245 opencv_contrib245 opencv_core245 opencv_features2d245 opencv_flann245 opencv_gpu245 opencv_highgui245 opencv_imgproc245 opencv_legacy245 opencv_ml245 opencv_nonfree245 opencv_objdetect245 opencv_photo245 opencv_stitching245 opencv_video245 opencv_videostab245
Add the built OpenCV library folder,
opencv_src_folder/Mingw_build/install/lib
to Library search path (-L).
打开 CMake-gui
- 将src文件夹设置为
opencv_src_folder
- 将构建文件夹设置为
opencv_src_folder/Mingw_build/
- 点击
configure
然后选择Eclipse CDT4 - MinGW MakeFile
下一步。 - 如果配置成功,点击
generate
。
- 将src文件夹设置为
打开命令行 (Windows + R)
- $
cd opencv_src_folder/Mingw_build/
- $
mingw32-make
:这将使用 MinGW 制作 openCV,所以请等待它完成! - 现在,让我们安装它 $
mingw32-make install
这将在 opencv_src_folder/Mingw_build/install/ 中安装 cmake
- $
添加
opencv_src_folder/Mingw_build/install/bin/
到您的系统路径并重新启动会话。日食配置:
- 转到 Eclipse CDT IDE,使用示例 OpenCV 代码创建 C++ 程序。
- 转到 Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes,并添加源 OpenCV 文件夹“opencv_src_folder/Mingw_build/install/include/”
转到 Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Libraries,然后分别添加到 Libraries (-l) 中:(不要忘记将 245 更改为它们用于 lib 文件的任何数字) opencv_calib3d245 opencv_contrib245 opencv_core245 opencv_features2d245 opencv_flann245 opencv_gpu245 opencv_highgui245 opencv_imgproc245 opencv_legacy245 opencv_ml245 opencv_nonfree245 opencv_objdetect245 opencv_objdetect245 opencv_photo24titch5 openvideo4
将构建的 OpenCV 库文件夹添加
opencv_src_folder/Mingw_build/install/lib
到库搜索路径 (-L)。
Testing :
测试:
you can use the following code as a test :
您可以使用以下代码作为测试:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
And last build your project !
最后构建你的项目!
Hope that helps !
希望有帮助!
回答by Shank
The above posts are really good and got me close but not all the way through. The following are some additional steps that I took that finally worked for me.
上面的帖子真的很好,让我很接近,但不是一路走来。以下是我采取的一些最终对我有用的额外步骤。
Tips for building and installing OpenCV
构建和安装 OpenCV 的技巧
NOTE:I built this in Windows 10 (Using Parallel's desktop on Mac but I don't think that should matter)
注意:我在 Windows 10 中构建了它(在 Mac 上使用 Parallel 的桌面,但我认为这无关紧要)
First, I used version 3.13.1 of Cmake and download the latest openCV repo (The above link takes you to an older version).
Make sure that every time you are building using Cmake-gui, you either clear the cache, and delete everything from that folder or make a new directory and build in this new directory (I personally did the second one).
Next, I downloaded the MinGW installer from here. But when installing, instead used the following configurations
首先,我使用了 Cmake 的 3.13.1 版本并下载了最新的 openCV repo(上面的链接会带你到一个旧版本)。
确保每次使用 Cmake-gui 构建时,您要么清除缓存,然后删除该文件夹中的所有内容,要么创建一个新目录并在此新目录中构建(我个人做了第二个)。
接下来,我从这里下载了 MinGW 安装程序。但是在安装的时候,改为使用了以下配置
After a lot of google searching and reading through github issues, I found out that someone was only able to build and install using this configuration of MinGW and so was I. Also, when you do install this version of MinGW and add this to path make sure that you delete any other version that is being currently used from the system path and delete the folder as well. When you run Cmake-GUI, just when you hit configure, it should show you that it built with this new MinGW libraries if you did everything correctly.
经过大量的谷歌搜索和阅读github问题后,我发现有人只能使用MinGW的这种配置来构建和安装,我也是。另外,当你安装这个版本的MinGW并将其添加到路径make时确保从系统路径中删除当前正在使用的任何其他版本并删除该文件夹。当您运行 Cmake-GUI 时,就在您点击 configure 时,它应该会向您显示它是使用这个新的 MinGW 库构建的,如果您做的一切正确的话。
Next, add to system path as usual and restart Command Prompt.
After configuring in Cmake-GUI the first time, also do the following:
Search for CMAKE_BUILD_TYPE and in its Value Column add 'Release' (without the quotes)
Search for WITH_MSMF and uncheck it.
Search for ENABLED_PRECOMPILED_HEADERS and uncheck it.
接下来,像往常一样添加到系统路径并重新启动命令提示符。
第一次在 Cmake-GUI 中配置后,还要做如下操作:
搜索 CMAKE_BUILD_TYPE 并在其值列中添加“发布”(不带引号)
搜索 WITH_MSMF 并取消选中它。
搜索 ENABLED_PRECOMPILED_HEADERS 并取消选中它。
Tips for Eclipse Configuration
Eclipse 配置提示
Everything for eclipse worked for me as mentioned in the above post, except that I had to explicitly add the
正如上面的帖子中提到的,eclipse 的一切都对我有用,除了我必须明确添加
--std=c++11
flag under Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Miscellaneous > Other Flags
--std=c++11
Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Miscellaneous > Other Flags 下的标志
Before adding this, every program with openCV library was giving me 1000s of errors.
在添加这个之前,每个带有 openCV 库的程序都给了我 1000 多个错误。
回答by ron wizzle
People following the top answer might get a problem during compiling, which looks like this:
遵循最佳答案的人可能会在编译过程中遇到问题,如下所示:
modules\highgui\CMakeFiles\opencv_highgui.dir\build.make:187: recipe for target 'modules/highgui/CMakeFiles/opencv_highgui.dir/src/window_w32.cpp.obj' failed
mingw32-make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/window_w32.cpp.obj] Error 1
CMakeFiles\Makefile2:2203: recipe for target 'modules/highgui/CMakeFiles/opencv_highgui.dir/all' failed
mingw32-make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
Makefile:159: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
If that happens, there is a very good answer to it here.
如果出现这种情况,有一个很好的答案来这里。
Posting it as a separate answer for visibility, for people coming from google (like myself) that have this issue, who would otherwise spend 20-30 minutes trying to google it (like I did).
将它作为一个单独的可见性答案发布,对于来自谷歌的人(比如我自己)有这个问题,否则他们会花 20-30 分钟来尝试谷歌它(就像我一样)。
Edit:
编辑:
in the modules/highgui/src/window_w32.cpp
在模块/highgui/src/window_w32.cpp
please add this lines between the #endif and the #include lines like this
请在 #endif 和 #include 行之间添加此行,如下所示
# pragma GCC diagnostic ignored "-Wmissing-declarations"
#endif
/*start new code lines*/
#if (_WIN32_IE < 0x0500)
#pragma message("WARNING: Win32 UI needs to be compiled with _WIN32_IE >= 0x0500 (_WIN32_IE_IE50)")
#define _WIN32_IE 0x0500
#endif
/*end new code lines*/
#include <commctrl.h>
#include <stdlib.h>
#include <string.h>