问题
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:
#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.
Building the project all the OpenCV functions get undefined references errors, such as:
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.
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 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"
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!
回答1:
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
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).
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 !
回答2:
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
NOTE: I built this in Windows 10 (Using Parallel's desktop on Mac but I don't think that should matter)
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
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.
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.
Tips for Eclipse Configuration
Everything for eclipse worked for me as mentioned in the above post, except that I had to explicitly add the
--std=c++11
flag under 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.
回答3:
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).
Edit:
in the modules/highgui/src/window_w32.cpp
please add this lines between the #endif and the #include lines like this
# 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>
来源:https://stackoverflow.com/questions/37238431/how-can-i-correctly-link-opencv-libraries-to-eclipse-on-windows