Ask Your Question

Revision history [back]

OpenCV 3.1 - Static Linking with Dynamic Referencing

Hello there,

I'm currently trying to deploy an application that relies on OpenCV 3.1 and I want to link OpenCV statically. I was able to compile OpenCV 3.1 using CMake on Windows 8 x64 and it created an OpenCV directory at C:\opencv\build\install that has static libraries at C:\opencv\build\install\x64\mingw\staticlib (with .a extension).

The problem I'm facing right now is that even though I only have static OpenCV libraries, when generating a CodeBlocks projects using CMake, it generates linking flags to dynamic libraries that are not there. These references appear on the file linklibs.rsp located under CMakeFiles/"Project Name".dir in the project folder.

If I do not temper with any of the files, the building process crashes. However, if I remove the references to dynamic OpenCV libraries inside the linklibs.rsp file that I mentioned, the process goes through and the building is successful.

What I want to know is if there is something missing in my CMakeLists file to keep these dynamic references off my linking files?

As an example, I'm trying to compile the DisplayImage example found in the OpenCV website.

The DisplayImage.cpp file:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
    if( argc != 2)
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! )                              // Check for invalid input
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;

The CMakeLists file:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Thanks for your time.

Regards, Marcos