Grigory Fedorov
Grigory Fedorov

Reputation: 53

How to set libs order in qmake?


We have a problem building out C++ software on Ubuntu Linux with qmake.
Problem is: we use some library, for example OpenCV, that can have different versions in one system.
qmake automatically add -L/usr/lib or -L/usr/lib/x86_64-linux-gnu to g++ arguments, and contents of LIBS variables after it.
So there conflicts with different versions of OpenCV, the system version is used, but we need custom one, located at our build tree. Are there any methods to change libs order in -L or something else to solve this problem?

Upvotes: 3

Views: 2042

Answers (4)

re-fused
re-fused

Reputation: 11

I have two OpenCV versions on my PC, one installed by default in /usr and another installed by compiling the sources in a custom dir (not /usr). The first worked just fine with Qt, the other didn't. I struggled a lot trying to make the Qt Creator work with my OpenCV compiled sources. So I added -L/opencv_lib_path but it always said 'undefined reference' for some OpenCV API I was using. It simply doesn't want to look there for the libs, it will look in LD_LIBRARY_PATH instead. I tried adding my opencv_lib_path to the LD_LIBRARY_PATH, no joy either. The only thing that worked was Frodon's solution, just add this in your Qt .pro file and it will work.

QMAKE_LIBDIR = /path_to_installed_opencv/lib

Upvotes: 0

Frodon
Frodon

Reputation: 3775

I had the same issue which I fixed by setting QMAKE_LIBDIR to the lib directory of the build tree. QMake automatically added the system library path after this value, thus allowing to correctly detect the desired libraries:

QMAKE_LIBDIR = /path/to/desired/opencvlib

Upvotes: 0

fche
fche

Reputation: 2790

Another hack is to exploit the LIBS = $(SUBLIBS) ... part of the Makefile qmake writes. Namely, invoke the generated Makefile with

make SUBLIBS=-L/path/to/your/opencv

Upvotes: 0

enderland
enderland

Reputation: 14185

There are two components to doing this:

First, you need to make sure to include them in your .pro file correctly. Do this with something like (this is from my current project):

LIBS +=      L${OPENCV_HOME}/lib \
            -lopencv_core \
            -lopencv_highgui \

You can replace the environment variable with whatever your path is. I've found it convenient to use environment variables like this because you also need header includes:

INCLUDEPATH += $$(OPENCV_HOME)/include/opencv2 \
               $$(OPENCV_HOME)/include/opencv \
               $$(OPENCV_HOME)/include

This allows you to create projects and build them correctly.

When you attempt to run them, however, you will likely run into all sorts of issues due to your app finding the wrong libraries (from system libraries like you say) - you need to set your LD_LIBRARY_PATH variable correctly. In this case I have a launch script (you can do this in your user profile or elsewhere) which contains:

export LD_LIBRARY_PATH=${OPENCV_HOME}/lib

Which then looks to that (as well as other) locations on the LD_LIBRARY_PATH first, before system libraries.

Upvotes: 1

Related Questions