Building on Windows
Prerequisites
Section titled “Prerequisites”- Visual Studio (any recent version with the “Desktop development with C++” workload). Community Edition is free. VS 2019 or VS 2022 recommended.
- CMake ≥ 3.9.1 — Download from
cmake.org. During installation, select “Add CMake to system PATH for all users.” - Git — Download from
git-scm.com. Enable “Git Bash” during installation. - Python 3.x + NumPy — Required only for Python bindings. Download from
python.org. Install NumPy withpip install numpy.
Method A — cmake-gui + Visual Studio (Recommended for Beginners)
Section titled “Method A — cmake-gui + Visual Studio (Recommended for Beginners)”-
Launch cmake-gui. Set “Where is the source code” to the
opencvdirectory and “Where to build the binaries” to a separateopencv/buildfolder. -
Click “Configure.” In the popup, select your Visual Studio version (e.g., “Visual Studio 17 2022”) and set the platform to
x64. Click Finish. -
Set the required variables in the CMake GUI:
OPENCV_EXTRA_MODULES_PATH→ path toopencv_contrib/modulesBUILD_TESTS→ONBUILD_PERF_TESTS→ONOPENCV_TEST_DATA_PATH→ path toopencv_extra/testdata
-
Click “Generate.” This produces an
OpenCV.slnVisual Studio solution file in the build directory. -
Open
OpenCV.slnin Visual Studio. Select the build configuration (Debug or Release) from the toolbar dropdown. -
Build
ALL_BUILD— Right-click theALL_BUILDproject in Solution Explorer → Build. This compiles everything. -
Run tests — After building, test binaries are in
build\bin\Debug\orbuild\bin\Release\depending on configuration.
Method B — CLI with Ninja (Recommended for CI / Advanced Users)
Section titled “Method B — CLI with Ninja (Recommended for CI / Advanced Users)”cmake -G Ninja ^ -DCMAKE_BUILD_TYPE=Debug ^ -DBUILD_TESTS=ON ^ -DBUILD_PERF_TESTS=ON ^ -DOPENCV_EXTRA_MODULES_PATH=..\opencv_contrib\modules ^ ..\opencv
cmake --build . --config Debug -j %NUMBER_OF_PROCESSORS%cmake -G Ninja ^ -DCMAKE_BUILD_TYPE=Release ^ -DBUILD_TESTS=ON ^ -DOPENCV_EXTRA_MODULES_PATH=..\opencv_contrib\modules ^ ..\opencv
cmake --build . --config Release -j %NUMBER_OF_PROCESSORS%Path Configuration After Build
Section titled “Path Configuration After Build”Without installing, DLLs must be accessible at runtime. Add the bin directory to PATH:
# PowerShell (temporary for current session)$env:PATH = "C:\path\to\opencv\build\bin;$env:PATH"
# Or permanently via System Properties → Environment VariablesFor Python bindings:
$env:PYTHONPATH = "C:\path\to\opencv\build\python_loader"Visual Studio IDE Tips
Section titled “Visual Studio IDE Tips”- IntelliSense will automatically detect include paths from the CMake-generated
.vcxprojfiles. - Debug builds produce libraries with a
dsuffix:opencv_world480d.dll. - Set the “Startup Project” to
opencv_test_coreor any other test to debug individual tests with F5. - Use the “Test Explorer” window (Test → Test Explorer) if CMake has registered CTest tests.