How to compile a standalone OpenCV executable?

c++, g++, linux, opencv

Solution

The program during compilation are dynamically linked to Shared Libraries (.so files) on our computer. The executable compiled use these shared libraries during run-time. But these shared libraries may not be present on other computers, hence might not be able to run the executable.

Solution to this will be to statically link Archive Libraries (.a files) instead of dynamically linking Shared Libraries. OpenCV does not distribute archive libraries as such. So one will have to compile archive library from the its source using `cmake -DBUILD_SHARED_LIBS=OFF`. This archive library can be used to create standalone executable.

Problem

I compile my my OpenCV programs as follows: ``` g++ `pkg-config --cflags opencv --libs opencv` <filename>.cpp ``` It works perfectly on my computer. Can I complile the shared libraries alone with the program so that it can be run on other computers which doesnt have opencv on it? If so, how do I do it?

Original source