27

I'm trying to build OpenCV 2.4.6 on Windows 8 in Visual Studio 2012. Having downloaded the source from https://github.com/Itseez/opencv I generate (leaving the default configuration) using cmake and then load the resulting file ALL_BUILD.vcxproj into Visual Studio 2012. I then try to build it. Several of the modules do indeed build (e.g. core, flann, imgproc, ...) but I am trying to build highgui so that I can use the PDB file for debugging my code (which fails to open a video file). The build errors start with this and many similar errors:

error C2039: 'max' : is not a member of 'std' C:\OpenCV2.4.6\3rdparty\openexr\Imath\ImathMatrixAlgo.cpp 1094 1 IlmImf

One fix for errors like this (e.g. in this answer) is to add #include <algorithm> to the failing files. I tried that on a few files and it seems to work but I'm nervous about locally changing the source for a popular library. It must build on Windows (you can download the binaries, though not the PDB files, from the OpenCV sourceforge site) so I would like to understand how to build it on my machine without changing the source.

Am I missing something out in the configuration step? Is there some path setting etc. missing on my machine? Why am I getting these errors and how should I fix them?

========== EDIT ==========

Looking at the directory path this appears to be a problem with one of the 3rd party dependencies, OpenEXR. Looking on Github it appears to be version 1.7.1 of OpenEXR that is used in OpenCV 2.4.6. The instructions in the OpenCV's Quick Start Installation on Windows state:

  • In case of the Eigen library it is again a case of download and extract to the D:/OpenCV/dep directory.
  • Same as above with OpenEXR.

so I downloaded the OpenEXR 1.7.1 source code release and extracted the resulting files putting the directory openexr-1.7.1 into C:\OpenCV2.4.6\dep.

Then I ran cmake and tried to build the resulting Visual Studio solution. Sadly I see the same errors.

Community
  • 1
  • 1
dumbledad
  • 16,305
  • 23
  • 120
  • 273

3 Answers3

93

I was stumbling on the same issue while compiling OpenEXR. Then I found the solution googling for openexr std::min.

There is an issue opened on OpenCV where it says that, when using VS2013 Preview, you must add the line #include <algorithm> in the file where you're using std::min and std::max.

I put that line into the files where these methods are called and voilà! Compilation succeeded.

dumbledad
  • 16,305
  • 23
  • 120
  • 273
Shodan
  • 1,058
  • 7
  • 8
  • Thanks for this @Shodan. Up to now I've resisted changing the source files. I reasoned that this is a popular library with lots of people building it on Windows so there must be a correct fix that does not involve source file edits. But your reference to an OpenCV bug report has me persuaded :-) @akazakov has a nice hint on how/where to add `#include ` over in [another answer](http://stackoverflow.com/a/19050715/575530). – dumbledad Oct 28 '13 at 09:09
  • It is odd that many of the issues I'm facing building OpenCV locally are only faced by others who are using Visual Studio 2013 while I am using Visual Studio 2012, but I'll try not to fret about that. – dumbledad Oct 28 '13 at 09:12
9

You probably need to #define NOMINMAX. Try putting it before any other includes.
If that helps, then put in the you project's preprocessor defines.

There's lots of info about this, just search for NOMINMAX. Here's one post about it.

Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
  • As I mentioned I do not want to edit the files as this is a download of a popular library. It must be possible to get it to build (as the authors surely did) without edits, or else the edits themselves would already be in the code? – dumbledad Oct 21 '13 at 12:44
  • 1
    You need to put it in your project, not the lib. This is an MSVC thing. – Adi Shavit Oct 21 '13 at 16:23
  • I don't have a project - I'm trying to build the Opencv library as downloaded from their github – dumbledad Oct 23 '13 at 09:19
  • I've added the `/D NOMINMAX` flag by [adding `NOMINMAX` to the property pages](http://stackoverflow.com/a/7881218/575530) of one of the failing projects `llmlmf` under `Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions` but builing the project still gives errors like `C2039: 'max' : is not a member of 'std'` and `C3861: 'max': identifier not found` – dumbledad Oct 27 '13 at 06:30
  • Sorry, I don't know then Try posting your question here: http://answers.opencv.org/questions/ – Adi Shavit Oct 27 '13 at 11:25
  • Thanks for trying Adi, I posted it there when I posted it here too: http://answers.opencv.org/question/22669/min-max-not-a-member-of-std-errors-when-building/ – dumbledad Oct 27 '13 at 12:44
2
#include <iostream> 
#include <algorithm>

Just include above 2 lines to the cpp file and the compilation error will disappear.

Pratap
  • 21
  • 1