Update 2019-05-25: Added notes to help with changes and possible issues caused with more recent versions of CMake.
Over the years I have written quite a few guides and tutorials that describe building OpenCV from sources for Windows, however they were mostly done using the GUI. It means you’d have to be using CMake GUI to set some parameters and then a few other actions and you’d end up building OpenCV using Visual Studio like any other VS solution. Even though it’s quite simple to build OpenCV like this, you can’t automate it and you need to perform all the required actions manually. In this post though, I’ll be sharing a few simple commands that can be put inside a batch script that will allow you to build OpenCV for any platform you like, simply by executing (or double clicking) that script!
First things first, make sure you have the prerequisites for building OpenCV. A rough list of requirements would include the following:
- Microsoft Visual C++ 2017 (Included in Microsoft Visual Studio Community 2017)
- CMake, latest version preferably (must be added to the PATH)
- Python, latest version preferably
After making sure you have all the requirements, we can start with describing the script for building OpenCV from sources. Assuming you have downloaded the latest OpenCV (currently 3.4.3) source codes from here and extracted into your preferred folder, you need to start by running the following commands from inside that folder:
mkdir "build_msvc2017_x86"
cd "build_msvc2017_x86"
cmake ".." -G "Visual Studio 15 2017"
This will create a folder named “build_msvc2017_x86” in your OpenCV source codes folder, then switch to that folder and run CMake to create a proper project for MSVC 2017 32-bit. In case you need the 64-bit version of OpenCV, you can replace the command starting with “cmake” with the following:
cmake ".." -G "Visual Studio 15 2017 64"
OpenCV allows packing all of its libraries into a single DLL file called “opencv_world###.dll”, where “###” is replaced by the OpenCV version number, in this case 343. If you want the single OpenCV World library, you should use the following CMake command instead:
cmake ".." -G "Visual Studio 15 2017 64" -DBUILD_opencv_world:BOOL=ON
Update 2019-05-25: In more recent versions of CMake, “64” in the script above needs to be replaced by “Win64”.
Note that any CMake value can be set with a similar command and by using the “-D” parameter followed by variable, type and value.
After the CMake process has ended and the proper projects are created, we can start building them by using the following commands:
cmake --build "." --config Debug --target ALL_BUILD
cmake --build "." --config Release --target ALL_BUILD
Note that we are simply starting the “ALL_BUILD” build target by using CMake. This target is responsible for building all OpenCV libraries in order and creating its DLL files for Debug and Release modes separately.
Besides the “ALL_BUILD” target, OpenCV also provides an “INSTALL” target that will help creating a proper OpenCV install folder. Here’s how it’s done from the command line:
cmake --build "." --config Debug --target INSTALL
cmake --build "." --config Release --target INSTALL
Following is the complete script for building OpenCV with MSVC 2017 32-bit and with OpenCV World mode. Copy and paste it into a *.BAT file in the root folder of the OpenCV source folder and run it to build OpenCV:
mkdir "build_msvc2017_x86"
cd "build_msvc2017_x86"
cmake ".." -G "Visual Studio 15 2017" -DBUILD_opencv_world:BOOL=ON
cmake --build "." --config Debug --target ALL_BUILD
cmake --build "." --config Release --target ALL_BUILD
cmake --build "." --config Debug --target INSTALL
cmake --build "." --config Release --target INSTALL
pause
The “pause” command at the end is to make sure the script doesn’t just go away after it’s completed. After all of the commands above have completed their work, you can expect to have your OpenCV installation (include headers, LIB files, DLL files and so on) inside the “install” folder under the “build_msvc2017_x86” folder which was created inside your OpenCV source codes folder.
For your questions you can use the comment box below.
Hi Amin, I have installed all the requirements but I still get this error when run the first cmake command:
CMake Error: Could not create named generator Visual Studio 15 2017 64″
I tried updating everything (CMake,Visual studio ..) but nothing would work. Help me please!!
Do you have Visual Studio 2017 installed on your computer? Are you sure it contains the complete set of tools required for building desktop applications? Please double check these first before updating anything.
Hi Mohamed,
This answer comes a bit late, but just in case, I updated this post to cover a possible cause for the issue you were facing.
You might want to give it a try.
Basically, in the generate line from the script you should run the following:
cmake ".." -G "Visual Studio 15 2017 Win64" -DBUILD_opencv_world:BOOL=ON
Hello Amin,
I had read your book ,I have a question to need your help.
I want to ask How to build help document that use for the Qt Creator(press F1 key and jump to the OpenCV function&Class).
thank you!
Hi Gong,
To be able to create documentations compatible with Qt Creator, you must create *.qch files and add them to Qt Creator from “Tools / Options” menu and then selecting “Help” and then “Documentation”.
But creating *.qch files requires a number of tasks to be taken care of first. Here is a link that includes all the necessary information for creating Qt-compatible help files:
http://doc.qt.io/qt-5/qthelp-framework.html
To cut it short, you need to make sure compatible inline documentation exists within OpenCV codes and then use “qhelpgenerator” to convert a Qt Help Project (*.qhp) to *.qch
Here is a link to “Qt Help Project” that describes how to create a correct *.qhp file:
http://doc.qt.io/qt-5/qthelpproject.html
I hope this helps you with your question.
And btw, you gave me a very nice idea 🙂 Hopefully I’ll provide OpenCV help files that are compatible with Qt Creator, very soon, so stay tuned!
thank you. I just want to know the way to build the OpenCV3.x.qch help document.
just for OpenCV3.x function&Class).
As mentioned before, you need to check the documentations I provided.
It’s only possible if OpenCV has documentations compatible with Qt.