Adding OpenCV 4.2.0 to Visual Studio 2019 project in Windows using pre-built binaries
Note: This is an update from a previous tutorial on how to add OpenCV 4.1.2 to Visual Studio 2019 in Windows. If you need to add OpenCV 4.1.2, please refer to the previous post.
The official documentation for OpenCV 4.2.0 still has vastly outdated contents on setting up OpenCV in Visual Studio (for C++). So, let’s take a quick dive into how to setup OpenCV 4.2.0 on Visual Studio 2019.
Environment
- Windows 10 (64-bit)
- Visual Studio 2019
Step 1: Download and extract the pre-built library
Download the latest binary from opencv’s Github repository. The latest version as of December 25, 2019 (Merry Christmas y’all) is 4.2.0 (opencv-4.2.0-vc14_vc15.exe). Release files for 4.2.0 are listed at https://github.com/opencv/opencv/releases/tag/4.2.0.
Run the downloaded .exe
file to extract the archive.
For the sake of uniformity, this tutorial will assume that you’ve extracted the contents to c:\
. If you’d like to use a different path, it is completely fine. You simply need to adjust the paths in the later sections of this post.
The total extracted size will be about 1.2GB. Check your c:\
to ensure that you have the new opencv
folder.
Step 2: Add to path
Add opencv’s bin
directory to path. If you’ve extracted the downloaded .exe
file into c:\
, the path to the bin
directory should be c:\opencv\build\x64\vc15\bin
.
Step 3: Create a project in Visual Studio 2019
In this step, we’re creating a new project in Visual Studio 2019. Note that the settings we set below are only applied to the new project. If you create a new project, you’ll need to repeat the steps below.
First, select Create a new project.
In the next page, select Console App. Note that you may have multiple variants of console apps (.NET Core, .NET Framework, etc). Select the one without parentheses. It should also have C++
, Windows
, Console
tags.
Wait, what if I don’t see the “Console App” option in the previous step?
If you don’t see the “Console App” option in “Create a new project” window, it’s probably because you haven’t installed the required components for C++ development.
Click on Install more tools and features, and install Desktop development with C++ workload module.
Configure your project and continue. You can name the project however you’d like.
Before we go any further, I want to outline the steps we’re taking and why we are performing each step.
- Set platform target to x64 — Pre-built binaries are built for x64 Windows platforms.
- Add to Include Directories — Tell the compiler how the OpenCV library looks. This is done by providing a path to the header files (
build/include
).¹ - Add to Library Directories — Tell the linker where it can find the lib files for different modules.
- Add Additional Dependencies — List
.lib
files for different modules. Note that we’re only going to list a single all-in-one file namedopencv_world
.
Let’s move on. First, set platform target to x64. Also, make sure “Debug” is selected.
Now, go to Project → YourProjectName Properties in the menu.
Once new window opens up, select VC++ Directories page on the left and click on Include Directories row. Once you see the down arrow on the rightmost part of the row, click on the arrow, and select <Edit…>.
In Include Directories window, add c:\opencv\build\include
.
Click OK. In the same tab, look for Library Directories. Again, click on the down arrow and select <Edit…>.
The VC++ Directories tab should look like below:
It’s time to list the module dependencies. As mentioned above, we are only going to add a single all-in-one module named opencv_world
.
The Linker tab should look like below:
We now should be ready to write some OpenCV code and see it in action.
Step 4: Check out demo code!
Copy and paste the code below and press F5 to run (or click on the Run button with the label “Local Windows Debugger”.
There we go. Have fun with OpenCV.
But wait — I’m still having issues!
Please go over the previous steps carefully to see if you’ve made a typo or missed a step.
Also, if you’re getting an build error with the message “LNK 1104: Cannot open file ‘opencv_world4xxd.lib’, go to the lib directory at C:\opencv\build\x64\vc15\lib
to check that the opencv_world420d.lib file exists. If it does, make sure the file name matches the additional dependency we’ve added to Linker Inputs.
If you run into any other issues, please feel free to let me know.
[1]: OpenCV Documentation. How to build applications with OpenCV inside the “Microsoft Visual Studio”