OpenCV C++ on Windows: Visual Studio Setup Guide

Set up OpenCV 4.12.0 for C++ development on Windows 11/10 using Visual Studio and CMake. Learn how to configure include/lib paths, linker inputs, runtime compatibility, and avoid common DLL errors.

  • Requirements: Windows 11/10 x64, Visual Studio 2019/2022 or Build Tools
  • Optional: CMake (for projects that use CMake presets)
  • Runtime: Use matching MSVC runtime and x64 toolset

TL;DR — C++ quick steps

  1. Install Visual Studio (Desktop development with C++ workload).
  2. Download and install OpenCV for Windows.
  3. Configure include/lib directories and linker inputs in your VS project.
  4. Ensure your PATH or app folder contains required OpenCV DLLs at runtime.

Need Python or GPU? See Python and CUDA.

Visual Studio configuration

1) Install the toolchain

  • Visual Studio 2019/2022 with Desktop development with C++ or MSVC Build Tools.
  • Target x64 (Win32/x86 is not supported by most modern builds).
  • Install CMake if your project uses it (recommended for portability).

2) Install OpenCV for Windows

  • Use the official installer for OpenCV 4.12.0.
  • Note the installation path (it contains include, lib, and bin directories).

3) Project include and library paths

  • Add the include directory to your project’s Additional Include Directories.
  • Add the lib directory to Additional Library Directories.
  • Add the appropriate OpenCV libraries to Linker → Input (e.g., combined world library for your config).

4) Runtime DLL availability

  • Ensure that OpenCV DLLs are available at runtime.
  • Either add the bin folder to your system/user PATH or place required DLLs next to your executable.
  • Match Debug vs Release and x64 vs x86 consistently across your project and libraries.

5) Optional: Using CMake projects

  • Point your CMAKE_PREFIX_PATH to the OpenCV installation so that your build system can find it.
  • Regenerate your build files after setting the prefix/path to ensure detection.

Common pitfalls and quick fixes

  • “DLL not found” at runtime: add OpenCV bin to PATH or deploy DLLs alongside the executable.
  • Unresolved externals: ensure you linked the correct libraries for your configuration (Release vs Debug) and platform (x64).
  • MSVC runtime mismatch: use the same toolset/runtime across dependencies; avoid mixing 2019 and 2022 libs.
  • Wrong architecture: verify that project, OpenCV, and all third-party libs are built for x64.

C++ FAQ on Windows

Which Visual Studio versions are supported?
Visual Studio 2019 and 2022 are commonly used, with x64 toolsets. Ensure the Desktop development with C++ workload is installed.
Do I need to build OpenCV from source?
No for most users. The official Windows installer includes prebuilt binaries. Build from source only for special modules or custom flags.
How do I fix “The program can't start because ... dll is missing”?
Add OpenCV bin to the PATH or copy required DLLs next to your executable. Ensure you run the correct configuration (Release vs Debug).
Can I enable GPU acceleration with C++?
Yes, with CUDA-enabled builds and matching NVIDIA drivers/toolkit. See the CUDA/GPU page for details.

Related pages