Search code examples
opencvcmakestatic-linkingopencv3.1

Building an OpenCV Application with Static Libraries


Edit:

Problem solved after a few steps. You can take a look at my post on the OpenCV Q&A page.

http://answers.opencv.org/question/152366/build-application-with-static-libraries/

I'm trying to statically link my C++ (CMake) application to run on another computer which does not have OpenCV installed.

So far,

  1. I built OpenCV from source by setting BUILD_SHARED_LIBS to OFF, so I can see library files under lib directory. (with .a extension)

  2. I edited the environment variable for the new build of OpenCV, my IDE does see include directories and files, so this step also seems ok.

  3. Added "-static" parameter to target_link_libraries command in CMake:

    target_link_libraries(HoGPeopleDetector ${OpenCV_LIBS} "-static")
    

Then rebuilt the application, EXE size increased to 1.6 MB from 200 KB. So I guess linking process was successful.

The application works well when I run it within my IDE. However, when I try to run it from the EXE file, it still expects DLL files. (libopencv_core310.dll and maybe more)

IDE: CLion 2017.1.2 OpenCV: 3.1


Solution

  • Problem solved after a few steps. You can take a look at my post on the OpenCV Q&A page.

    http://answers.opencv.org/question/152366/build-application-with-static-libraries/

    Overall, it was "remember to clear your CMake cache file between each invocation of cmake" and to move/copy the opencv_ffmpegXXX.dll (310 in my case), put it in the same directory with your EXE.