Compilation and Configuration of OpenCV 3.3.0 with Qt 5.9.1, CMake 3.18.5 and Visual Studio 2015
Pre-configuration Preparation
Install Visual Studio 2015, Qt 5.9.1 and CMake 3.18.5, then extract the OpenCV 3.3.0 source package to a local directory. Configure the required system environment variables before proceeding with compilation.
If you use the CMake executable installer, check the "Add CMake to the system PATH for all users" option during installation to automatically configure environment variables. If you use the zipped portable CMake package, extract it first, then manually add the full patth to its bin directory (e.g., C:\Program Files (x86)\cmake-3.18.5-win64-x64\bin if cmake-gui.exe is stored in this path) to the system Path variable.
For consistency in subsequent steps, set the OpenCV extraction path to C:\dev\opencv and the Qt installation path to C:\Qt\Qt5.9.1.
CMake Build Configuration
Create a new dedicated build output directory named cmakeBuildx64 under the OpenCV root directory to seperate ganerated build files from the original source code. Launch CMake-GUI, set the source code path to C:\dev\opencv\sources, and the build output path to the newly created C:\dev\opencv\cmakeBuildx64. Select "Visual Studio 14 2015 Win64" as the target generator, then run the first configuration operation.
After the first configuration completes, check the BUILD_opencv_world option to bundle all OpenCV modules into a single library file for easier subsequent deployment. Run the configuration operation again, then click Generate to create the Visual Studio solution files.
Compile OpenCV with Visual Studio 2015
Open the generated OpenCV.sln file located in C:\dev\opencv\cmakeBuildx64 using Visual Studio 2015. Two compilation methods are available:
- Batch build: Navigate to Build > Batch Build in the top menu, check the
INSTALLproject under the Debug|x64 configuration (you can check the Release|x64 configuration later if you need release mode binaries), then start the build. This process usually takes 10 to 20 minutes depending on hardware performance. - Single target build: Locate the
INSTALLtarget under the CMakeTargets folder in the Solution Explorer, right click and select "Only Build > Only Build INSTALL". Note that this method may throw compilation errors on some environments, so the batch build method is recommended.
After compilation completes, add the path C:\dev\opencv\cmakeBuildx64\install\x64\vc14\bin to the system Path environment variable to allow applications to find OpenCV runtime DLLs.
Create Qt OpenCV Configuration PRI File
Create a file named opencv_x64.pri under C:\dev\opencv with the following content, adapted for 64-bit builds:
INCLUDEPATH += C:/dev/opencv/cmakeBuildx64/install/include
CONFIG(debug, debug|release) {
LIBS += -LC:/dev/opencv/cmakeBuildx64/install/x64/vc14/lib \
-lopencv_world330d
} else {
LIBS += -LC:/dev/opencv/cmakeBuildx64/install/x64/vc14/lib \
-lopencv_world330
}
Qt Test Project Validation
- When creating a new Qt Widgets Application, select the MSVC2015 64-bit kit as the build target.
- Add the following line to the end of the project's .pro file to import the pre-configured OpenCV settings:
include(C:/dev/opencv/opencv_x64.pri)
- Modify the main.cpp file as follows, replace the placeholder string in
imreadwith the full path to a local test image:
#include "mainwindow.h"
#include <QApplication>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
int main(int argc, char *argv[])
{
QApplication app_instance(argc, argv);
MainWindow main_window;
main_window.show();
cv::Mat test_img = cv::imread("your_local_test_image_path.jpg");
cv::imshow("OpenCV Test Window", test_img);
return app_instance.exec();
}
Run qmake first after modifying the .pro file, then build the project and run it. Skipping the qmake step often leads to missing dependency errors even if all configuration steps are completed correctly.
Comon rc.exe Error Fix
If you encounter an error that Qt cannot find rc.exe during the build process, use the following solution based on you're target architecture:
- For 32-bit builds: Copy
rc.exeandrcdll.dllfromC:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x86to theC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bindirectory of your VS2015 installation. - For 64-bit builds: Copy
rc.exeandrcdll.dllfromC:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64to theC:\Qt\Qt5.9.1\5.9.1\msvc2015_64\bindirectory of your Qt installation.