Building OpenCV 4.3.0 with Visual Studio 2019 Community

This is another one of those notes for myself which might be useful to you as well so I’m sharing it here. If you want to easily download and build OpenCV 4.3.0 with a simple PowerShell script, the this guide is for you.

The requirements are pretty straightforward:

  • PowerShell (latest version)
  • Visual Studio 2019 Community Edition
  • Python 3.x (Install latest version as part of Visual Studio)
  • numpy module for Python
  • CMake
  • Git

Now, simply paste the following into a text file with ps1 extension and run it using PowerShell (as an Administrator):

$env:Path += ";C:\Tools\CMake_64\bin"
$env:Path += ";C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64"
$env:Path += "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\"
Import-Module "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
Enter-VsDevShell 534f7fd3

Set-Location "C:\some_build_folder"
git clone --branch 4.3.0
Set-Location opencv
mkdir build
Set-Location build

cmake ".." -G "Visual Studio 16 2019"

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

You can replace the cmake generator (Visual Studio 16 2019) with other generator types or replace OpenCV version number and reuse it with other projects as well.

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.