Search code examples
opencvqt4static-librariesstatic-linking

How to statically link against OpenCV in Qt project


I would like to link against OpenCV library (debian squeeze package) in my Qt project. The project is a plain C++ project using no Qt libraries, it just uses Qt build system (qmake). to generate MakeFile.

I think OpenCV library in of package is compiled both statically and dynamic, because there are .so objects and .a objects in /usr/lib. I'm looking for linkage options of Qt project file to tell the build system to look for static libraries. I've also tried adding LIBS+= -static -lcv ,... but didn't work.


Solution

  • I had lots of problems trying to deploy a project that used OpenCV and QT. Static linking ended up being a horrible rabbit hole that maybe i was not clever enough to figure out, but we came up with what I feel to be a better alternative. check it out if interested. It involves the craziest command I have ever seen written :)

    Statically compiling a project that uses many independent libraries can be prohibitively tricky. There can also be weird legal problems with building other libraries into your binary if they are not GPL'ed. Instead we found a simpler method for sharing a binary and its dependencies in one neat little package.

    First build a normal release binary as if you were just normally developing Next go into this release directory and run this command. It will copy every dependency needed by the binary to the folder of your choice!

    cp `ldd NAMEOFYOURRELEASE | sed -re s/^.+\=\>// | sed -re 's/^(.+) \(.+\)/\1/'` FOLDERNAMETOPUTLIBRARIES/
    

    for NAMEOFYOURRELEASE put the name of the executeable binary file that you created,

    and for FOLDERNAMETOPUTLIBRARIES put the name of a folder that you would wish to copy all the dependencies into 3. Now we need to tell the executable where to look for the dependencies when it is on a strange new computer. Go into your .pro file for your project and add these two lines

    QMAKE_LFLAGS += -Wl,--rpath=\\\$\$ORIGIN/FOLDERNAMETOPUTLIBRARIES
    QMAKE_LFLAGS_RPATH=
    

    Now you should be able to move this binary and its folder of dependencies wherever you want!