How to build OpenCV from source for VC14

OpenCV 2.4.11 users may have noticed that it does not provide pre-built binaries (Libs and DLLs) for VC14 (Visual Studio 2015) Unfortunately it is also the case with Qt 5.5.1 (At least it’s true at the time of writing this article) so if you are going to use either one of them you have to build it yourself.

I was also faced with this obstacle so I decided to write a guide for anyone who wishes to built OpenCV for VC14.To do that, just follow the steps below:

Requirements:

OpenCV source codes

CMake

Visual Studio 2015 Community

Note 1: This guide is already tested with CMake 3.4.1, OpenCV 2.4.11 and Visual Studio 2015 but there is a very high chance that it would work with later versions too, but I’m not sure about previous versions, you have to test it for yourself.

Note 2: You are not required to use the same folders. You may extract OpenCV sources somewhere different and also install the binaries to another place but throughout my guide and in the pictures you’ll see the same paths.

So, here it is.

  • First run CMake and set the required folders.
    • Source codes are in the existing folder that can be found inside OpenCV folder after its installation
    • The second folder you see below is the one that will be created during the build.

 

2015-12-04 (2)



  • Click yes after you are warned with the message shown below.

2015-12-04 (3)

  • Now click configure. Then select “Visual Studio 14 2015” from the list. Note that you can also select WIN64 for a 64 build but in this guide I am creating a x86 build.

2015-12-04 (4)

  • Wait for the process to finish.

2015-12-04 (6)

  • Do not change any settings at this stage, just click on Generate.

2015-12-04 (12)



  • After generate is finished, close CMake and run Visual Studio Community 2015. Then open “OpenCV.SLN” file from the build directory.

2015-12-04 (18)

  • After the solution is opened, just right click on the solution’s name and Rebuild.

2015-12-04 (19)

  • After building for Debug mode ends change the build type to release, then right click and run Rebuild again.

2015-12-04 (22)

  • This concludes this tutorial. You have built all required libraries for OpenCV to use with Visual Studio 2015 (Visual C++ 2015) or Visual C++ 14.0. You can find the LIB and DLL files in separate folders. Debug and Release build folders are also created inside them.

2015-12-04 (24)



If you have any questions just put a comment below.

37 Replies to “How to build OpenCV from source for VC14”

  1. Hello, thank you so much for this tutorial !!!

    I’m trying to build it with CMake 3.8.0 and OpenCV 2.4.13 and MSVC 2015.
    Everything works fin, I can build in debug with no errors.
    However, there is one subproject that doesn’t compile in release (opencv_highgui). There are 543 errors all related to LNK2001 or LNK2019, for instance:
    Error LNK2001 unresolved external symbol “__declspec(dllimport) public: __thiscall QString::QString(class QString const &)” (__imp_??0QString@@QAE@ABV0@@Z) opencv_highgui C:\opencv2413msvcbuild\modules\highgui\moc_compilation.obj 1

    Any idea ?

    Thanks,

    Alex

    1. Forget about it, it works.
      I started everything from scratch and it works.
      I thus confirm that it works with CMake 3.8.0 and OpenCV 2.4.13 and MSVC 2015
      Thank you so much again for this tutorial !!

  2. Hi Amin,
    First, thanks for the reply!
    Second, when I try to rebuild my solution, I`m getting several errors (59) and Warnings (45). I tried to clean my solution and rebuild, but the errors still there. They are marked as LNK1104. I google it but didn’t get solid responses.
    Thanks

  3. Hi there Amin, I have been reading this tutorial, “How to build OpenCV”.
    I use the following software :
    Cmake 3.7.1
    Visual Studio Community 2015 ( ver 14.0.25431.01 Update 3 )

    My wish is to compile the 32bit version of OPENCV.
    ( so I want to use either OpenCV version 2.4.13 or version 3.2, whichever one works.)

    My source directory = C:\code\opencv-2413\sources
    My destination directory = C:\code\opencv-2413\build\x86\vc14

    Within CMAKE, I get lots of complex errors after touching “Configure”.
    [ These are those errors: ]
    Screenshot link : http://oi68.tinypic.com/2ptx72e.jpg

    “CPACK_PACKAGE_VERSION does not match version provided by version.hpp”

    “CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
    Please set them or make sure they are set and tested correctly in the CMake files:
    opencv_dep_CUDA_cufft_LIBRARY”
    ….

    Is it possible to compile the OPENCV source code without using CMAKE ?
    CMAKE is apparently a disaster, and I can never get it to work.

    Thanks for reading this. From Andy

    1. Let’s focus on solving the issue instead of a workaround. I’m a little bit in the dark here but first thing that comes to mind is that you should try again with a lower version of CMake. I’ll recommend using 2.8, just try it and let me know what you see and make sure you delete the whole build folder first, before rebuilding. I’ve just recently built with CMake 3.4 so I can confirm if everything you’ve got is from official sources and if everything’s correctly installed then you should be able to build.

      https://cmake.org/files/

  4. hello Dear Amin
    i cant speak english very well but i hava a problem about opencv !
    i wrote my first program in VS 2015 to learn opencv by code :

    #include
    #include
    using namespace cv;
    using namespace std;

    int main() {

    Mat im = imread(“cameraman.tiff”, 0);
    if (im.empty()) {

    cout << "Can not load image !" <—— Build started: Project: Project1, Configuration: Debug Win32 ——
    1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppBuild.targets(366,5): warning MSB8003: Could not find WindowsSDKDir variable from the registry. TargetFrameworkVersion or PlatformToolset may be set to an invalid version number.
    1> Opncv.cpp
    1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\crtdefs.h(10): fatal error C1083: Cannot open include file: ‘corecrt.h’: No such file or directory
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    please help me if you can !

    im using visual studio 2015 update 1 ( a 32bit version i think) and open cv 3.1.0 (a 64bit version).
    do you think that the problem is incompatibility between these ?
    my windows is also 7 64bit service pack 1

    1. #include
      #include
      using namespace cv;
      using namespace std;

      int main() {

      Mat im = imread(“cameraman.tiff”, 0);
      if (im.empty()) {

      cout << "Can not load image !" << endl;
      return -1;
      }

      imshow("image", im);
      waitKey(0);

      return 0;
      }

      this is the complete face of the code

      1. Your code seems to be OK. But this is not enough. You need to add the references and includes to your project properly.

    2. Hi Diako, your English is quite fine. (better than mine perhaps 😉 )
      And yes, the incompatibility will definitely affect it.

  5. Thank you for your tutorial. I met a error in configuration process, project files may be invalid. Can you suggest to solve this problem?

    1. Make sure you select the correct generator during the configuration. If you still face the same problem then please try to share more info about how you are trying to configure and build so that I can help you with the process.

  6. Q1:
    Is that ok with fail item after building for Debug and Release mode? I also see the fail item in your photo.

    Q2:
    If I do not change any settings at this stage, just click on Generate.

    is the result fit my environment? ex: if I don’t have GPU but I didn’t mark out the option

    1. A1: You can’t continue if it fails. The message you see in the picture is just a safe warning. You can always continue building if you pay attention to warnings and messages.

      A2: You can just click generate. Just note that if there are missing headers for any of the options, it might fail. And, you can build with any options you want and use with some other options.

      I suggest playing around with different options if you are trying to learn and not in a hurry. There are priceless lessons in it.

  7. Done these process. So If i run any projects it will automatically show me the librariers or what?

    1. Not sure what you mean exactly. But if you mean libraries will be automatically added to your project, no that won’t happen. You have to set all of them manually. Did I get it right? 🙂

  8. Hi Amin, Thanks for your tutorial which helped me a lot. However, I am facing a problem where VS crashes during the rebuild of both the debug and release builds. The last words I could catch on the process window was about error with “cryptoAPI”.

    Is there an error logfile I can access and what do you think might be the problem?

    Thanks.

    Michael

    1. Hi Michael,
      Which OpenCV version you are using?
      Are you trying a build mixed with something else? Or might be there is some kind of conflict between folders?!

      To be honest I have never faced such an error and to me it doesn’t seem to be related to OpenCV.

      1. Hi,

        Thanks for the reply. I managed to solve the problem. Seems like it may have been a faulty install of VS 2015, specifically the build tools. I reinstalled it and the problem is gone. 🙂

  9. Hi Amin, Thanks for this tutorial. I was stuck for a while till I followed the instructions. However, I ran into a small problem right towards the end when rebuilding with VS 2015.

    Everytime I tried to rebuild, the process crashes my computer. I recall seeing errors about “cryptoAPI” during the build just before the crash, but it was too fast to capture the entire error. Is there an error log I can retrieve and what might be the problem? It’s the same with rebuilding the debug and release, except the crash happens much earlier during release.

    Could you advise? Thank you so much.

  10. opencv 2.4.12 cmake 3.5.1 error message.

    Severity Code Description Project File Line Suppression State
    Error C2610 ‘std::tuple::tuple(const std::tuple &)’: is not a special member function which can be defaulted opencv_perf_gpu C:Program Files (x86)Microsoft Visual Studio 14.0VCincludetuple 328

    1. Please let me know what parameters you are using to build OpenCV. Most probably you are using a parameter which has dependencies that are not met on your system. I suggest going for only the ones that you need and you are sure that have dependencies on your computer.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.