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
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.