Building it in Linux and macOS is trivial thanks to package manager. However, building it in Windows is a little bit tricky.
Don't forget to clone the submodules
git submodule update --init --recursiveWhen I say "install" I also mean "add to your PATH environment variable".
- Install Ninja. Extract the zip file and add the
folder to your
PATHenvironment variable. - Install CMake by Installer.
- Install Visual Studio Build Tools and select and install Desktop development with C++ in the installer.
- Install LLVM. Find Windows
installer called
LLVM-<version>.win64.exe. (likeLLVM-17.0.1-win64.exe) I assume you install it toC:\Program Files\LLVMsoclangshould beC:\Program Files\LLVM\bin\clang.exe. - Install openCV binary from release. (like
opencv-4.8.0-windows.exe). See OpenCV Windows Installation.
You DO NOT NEED MSYS2 since no MingGW should be used.
If you see clang: warning: unable to find a Visual Studio installation; try running Clang from a developer command prompt [-Wmsvc-not-found] when compiling then you should check if Visual Studio Build Tools have been installed correctly.
You could still try
mingw-w64-x86_64-opencv
or
mingw-w64-ucrt-x86_64-opencv
in MSYS2 in a MinGW environment if you're targeting MinGW but they are not binary compatible with MSVC.
Run the executable and extract the files to a folder. I prefer C:\opt\opencv.
opencv
├── build
├── LICENSE_FFMPEG.txt
├── LICENSE.txt
├── README.md.txt
└── sourcesAdd C:\opt\opencv\build\x64\vc16\bin to your PATH environment variable. PATH acts like LD_LIBRARY_PATH in Linux here
so missing this step will cause dll not found error. In my machine even no error was raised so I have to check the dynamic linking manually using lucasg/Dependencies.
See also
- MSYS2 Environments
- CMakeLists for OpenCV that installed using Vcpkg
- Use Windows Terminal with MSYS2
- lucasg/Dependencies An open-source modern Dependency Walker (like
lddin Linux) - libc++ 与 libstdc++ 是什么关系?
- C/C++标准库
mkdir build
cd build
cmake -DOpenCV_DIR=C:/opt/opencv/build/x64/vc16/lib -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM/bin/clang++.exe" -DCMAKE_C_COMPILER="C:/Program Files/LLVM/bin/clang.exe" -G Ninja ..
ninja -j (Invoke-Expression [Environment]::ProcessorCount)See .vsode/settings.windows.jsonc. Change it to .vscode/settings.json to be your workspace configuration.
sudo apt install build-essential cmake ninja-build
sudo apt install libopencv-devpacman -S base-devel cmake ninja
pacman -S opencvThis is a typical out-of-source build. Trivially, you can run the following commands:
mkdir build
cd build
cmake .. -G Ninja
ninja -j$(nproc)