Build OpenCV With Visual Studio and CMake CLI

May 17, 2023 • C++ OpenCV Visual Studio CMake • 3 minutes to read • Edit

Build OpenCV With Visual Studio and CMake CLI

In the previous post, we have seen how to build OpenCV with Visual Studio and CMake GUI. In this post, we will see how to build OpenCV with Visual Studio and CMake CLI.

Compared to the previous post, this should be straight forward. We will be using the same CMake settings as before. The only difference is that we will be using the CMake CLI instead of the GUI and we will also use Ninja.

Why Ninja?

You might be wondering: why use Ninja? Ninja is a lean build system that prioritizes speed. It stands out from other build systems in two significant ways: it’s designed to have its input files generated by a higher-level build system (like CMake in our case), and it’s optimized to execute builds as quickly as possible. What’s more, Ninja is cross-platform, so whether you’re on Windows, Linux, or macOS, you can utilize its capabilities. If efficiency and speed are important to you in your build process, Ninja is an excellent tool to consider.

Prerequisites

Before we begin, you need to have the following on your computer:

  1. Visual Studio 2022 Community with C++ support
  2. CMake - I am using CMake 3.24.
  3. OpenCV - I am using OpenCV 4.7.0.
  4. Python - I am using Python 3.10.
  5. Ninja - I am using Ninja 1.11.1.
  6. Command Prompt.

Step 1: Set the Environment Variables - Command Prompt

Fortunately for us, Visual Studio 2022 comes with a script that sets the environment variables for us. We can find the script in the following location:

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat

Or

C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat

All you need to do is run the script in the command prompt. This will set the environment variables for us. To do this, open the command prompt and run the following command (I am using Command Prompt, to use with PowerShell see https://stackoverflow.com/questions/2124753/how-can-i-use-powershell-with-the-visual-studio-command-prompt):

cd "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build"
.\vcvars64.bat

Step 2: Build OpenCV

Now that we have set the environment variables, we can build OpenCV. In the OpenCV source code folder, create build, install directory and run cmake command. We will be using Ninja to build OpenCV. To do this, open the command prompt and run the following commands:

cd opencv
mkdir build
cd build
mkdir install

Now, let’s build it. Run the following command:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=<your desired install path> -D BUILD_opencv_world=ON -G "Ninja" ..
ninja
ninja install

Replace <your desired install path> with the actual path where you want to install OpenCV. For example, it could be C:/Downloads/opencv/install.

Conclusion

And there we have it! We’ve successfully navigated through the process of building OpenCV with CMake CLI, Ninja, and Visual Studio. This method offers both efficiency and flexibility, allowing you to tailor your OpenCV build to your specific needs. If you encounter any hiccups along the way, don’t hesitate to go back and check your setup. Keep learning, keep exploring, and enjoy your journey in the world of coding!




comments powered by Disqus