In this article, we explain how to build applications with OpenCV using Visual Studio. We will be using Visual Studio 16 2019.
If you write code on Windows, there is a high probability that you work on Visual Studio. It is used to develop computer programs, websites, web apps, web services, and mobile apps.
- Step 0: Prerequisites
- Step 1: Create an Empty C++ Project
- Step 2: Add a new C++ file to project
- Step 3: Setup Solution Platform
- Step 4: Set Additional Include Directories
- Step 5: Set Additional Library Directories
- Step 6: Set Additional Dependencies
- Step 7: Set Environment
- Step 8: Implement the Project
- Step 9: Build and Run the Project
Step 0: Prerequisites
Throughout the post, we assume that you have installed Visual Studio 16 2019, and have installed OpenCV 4.5.0 in C:\
using the installers. If you have any problems with the OpenCV installation, please check the earlier post.
Step 1: Create an Empty C++ Project
First, we start by opening Visual Studio and creating an empty Windows console project.
- Start Visual Studio
- Click on Create a new project
- Select Empty Project and click on Next
- Specify Project Name and Project Location
- Click Create
Step 2: Add a new C++ file to project
We can now start adding new .cpp
files or import existing code.
- Right-click on Source Files, then click on Add→New Item
- Click on C++ File (.cpp), specify Name and Location of the cpp file and click Add
Step 3: Setup Solution Platform
The files which we have provided work for x64 configuration.
- Set Solution Platform as x64
- Right-click on Project and click Properties
Step 4: Set Additional Include Directories
We give paths to the OpenCV header files. This will, in turn, tell the compiler how the OpenCV library looks.
- Set All Configuration and x64 Platform
- Go to C/C++ → General → Additional Include Directories and Click Edit
- Browse to add folders
- Navigate and select the OpenCV include folder
- Click OK and then Click Apply
Step 5: Set Additional Library Directories
Now we give paths to the OpenCV library files. This will tell the linker where to get the functions or data structures of OpenCV when needed.
- Set All Configuration and x64 Platform
- Go to Linker → General → Additional Library Directories and Click Edit
- Browse to add folders
- Navigate and select the OpenCV lib folder
- Click OK and then Click Apply
Step 6: Set Additional Dependencies
We need to list the .lib
files for the modules which will be used.
Debug
- Set Configuration to Debug and x64 Platform
- Go to Linker → Input → Additional Dependencies and Click Edit
- Specify the list of all lib file for Debug Mode
- If you have installed OpenCV using the Installers, you will find the list of additional dependencies for Debug mode in the root of your OpenCV installation (debug_files_list.txt)
- If not, you will have to type all the library files names in the text box
- Click OK then Click Apply
Release
- Set Configuration to Release and x64 Platform
- Go to Linker → Input → Additional Dependencies and Click Edit
- Specify the list of all lib files for Release Mode
- If you have installed OpenCV using the Installers, you will find the list of additional dependencies for Release mode in the root of your OpenCV installation (release_files_list.txt)
- If not, you will have to type all the library files names in the text box
- Click OK then Click Apply
Step 7: Set Environment
- You don't have to do this step if you have used OpenCV installers. This step is required if you have installed OpenCV from source.
- Set All Configuration and x64 Platform
- Go to Configuration Properties → Debugging → Environment and Edit the field
- Specify the address to the OpenCV bin folder containing all
.dll
files, by entering the following value in Environment field
PATH=C:\OpenCV\x64\vc16\bin;%PATH%
- Click Apply then Click OK
Step 8: Implement the Project
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
cout << "Hello, World!" << endl;
cout << "OpenCV version is " << CV_VERSION << endl;
return 0;
}
Step 9: Build and Run the Project
- Select your choice of Solution Configuration
- Build the project by pressing
Ctrl+B
or navigate to Build → Build opencv_example_project - To run the project With/Without Debugging press
F5/Ctrl+F5
, or Navigate to Debug → Start Debugging/Debug → Start Without Debugging
Summary
In this post, we learned how to configure a Visual Studio project to use OpenCV. First we set the Additional Include Directories, which is required for the #include
commands- this tells the compiler how the OpenCV library looks. We also set Additional Library Directories- which tells the linker where to get the required OpenCV functions and data structures from. We configured Additional Libraries for Debug and Release mode separately. Finally, we learned how to set the path environment variable for the .dll
files and build and run the code.