OpenCV Build Scripts for Windows, macOS and Linux

Following up on this post from last year, I’d like to share a couple of simple scripts that you can use to build OpenCV 3 or 4 on Windows, macOS and Linux operating systems. Needless to say, they require CMake and proper build tool sets on each platform.



before going into the details of each script, note that you need to run these commands from the OpenCV source codes root folder. I’d recommend copying them into a single script file and executing them.

Here is the first script I use to build OpenCV for Windows and MSVC 2019 x64:

mkdir "build_msvc2019_x64"
cd "build_msvc2019_x64"
cmake ".." -G "Visual Studio 16 2019" -A x64 -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

Here’s another one that can be used to build OpenCV for Windows MSVC 2019 x86:

mkdir "build_msvc2019_x86"
cd "build_msvc2019_x86"
cmake ".." -G "Visual Studio 16 2019" -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


In case of MSVC 2017, the generation is a bit different, so the script actually looks like this for x64:

mkdir "build_msvc2017_x64"
cd "build_msvc2017_x64"
cmake ".." -G "Visual Studio 15 2017 Win64" -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

And here is the same script for MSVC 2017 x86:

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


As for macOS and Linux, the process is way simpler. Here is what you need to run on macOS and Linux operating systems in order to build OpenCV from source codes:

mkdir "build"
cd "build"
cmake ".." -G "Unix Makefiles" -DBUILD_opencv_world:BOOL=ON
make
install

Before finishing this tutorial, I’d like to make a couple of small notes for macOS users. First, if you can’t find cmake command line tool you can check out the contents of the CMake GUI app. Inside there is a bin folder where contains cmake command line tool for macOS.

This concludes our guide for building OpenCV using simple scripts on multiple different platforms. Post your questions below if anything’s unclear.



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.