Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Compilation and Configuration of OpenCV 3.3.0 with Qt 5.9.1, CMake 3.18.5 and Visual Studio 2015

Tech 1

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:

  1. Batch build: Navigate to Build > Batch Build in the top menu, check the INSTALL project 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.
  2. Single target build: Locate the INSTALL target 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

  1. When creating a new Qt Widgets Application, select the MSVC2015 64-bit kit as the build target.
  2. 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)
  1. Modify the main.cpp file as follows, replace the placeholder string in imread with 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.exe and rcdll.dll from C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x86 to the C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin directory of your VS2015 installation.
  • For 64-bit builds: Copy rc.exe and rcdll.dll from C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64 to the C:\Qt\Qt5.9.1\5.9.1\msvc2015_64\bin directory of your Qt installation.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.