31

This is a bit of a newbie question. I am trying to add the OpenCV libraries to a QT project.

This question says the link flags are given by

pkg-config --libs opencv

If I paste the command line output into the project file like:

LIBS += -L/usr/local/lib -lml -lcvaux -lhighgui -lcv -lcxcore

then everything compiles fine, but now this isn't portable. How can I simply reference the output of the command?

Update: Tried Ken Bloom's suggestion, but it won't compile. The actual generated compiler commands are

# How it should be, at least on my machine
g++ -o QOpenCVTest main.o qopencvtest.o moc_qopencvtest.o -L/usr/lib -L/usr/local/lib -lml -lcvaux -lhighgui -lcv -lcxcore -lQtGui -lQtCore -lpthread

# with CONFIG and PKGCONFIG
g++ -o QOpenCVTest main.o qopencvtest.o moc_qopencvtest.o -L/usr/lib -lQtGui -lQtCore -lpthread
Community
  • 1
  • 1
MVG
  • 343
  • 1
  • 3
  • 6
  • What version of qt and qmake do you have ? Ken's suggestions should work but im not sure at which version of qt those where added. Also, could you post your full pro file as there could also be bugs that cause issues such that you see. – rasjani Aug 21 '10 at 19:30

5 Answers5

44
CONFIG += link_pkgconfig
PKGCONFIG += opencv

(I got this answer from http://beaufour.dk/blog/2008/02/using-pkgconfig.html)

Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
  • I think this is on the right lines but it doesn't quite work. I'll update the question. – MVG Aug 19 '10 at 00:12
  • Maybe the site I got it from was wrong. I haven't tested it myself. – Ken Bloom Aug 19 '10 at 00:37
  • 1
    It should work just fine on 4.6 qt atleast (where i've tested and still using this). Only "gotcha" is that, if this opencv library is installed *after* qmake has created makefile once (and make file still exists), running make or even qmake itself doesn't make things magically work. So, make distclean && qmake and new makefile should be created with proper libs for compilation. – rasjani Aug 21 '10 at 19:45
  • @rasjani: Thanks. A lot of the time, I'm just good at googling. Thanks for filling in the details. – Ken Bloom Aug 22 '10 at 03:21
  • If got `opencv development package not found` @SteveEng's answer solved it! – JustWe May 17 '19 at 03:04
  • if it doesn't work, try adding `QT_CONFIG -= no-pkg-config` – Minh Nghĩa Jul 25 '19 at 03:22
10

Ken's answer worked great. I just had to remove the spaces on either side of the += first.

CONFIG+=link_pkgconfig PKGCONFIG+=opencv
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Salida Software
  • 446
  • 5
  • 6
  • For a mac with opencv installed via macports use : LIBS+=/opt/local/lib/libcxcore.dylib LIBS+=/opt/local/lib/libcvaux.dylib LIBS+=/opt/local/lib/libcv.dylib LIBS+=/opt/local/lib/libhighgui.dylib LIBS+=/opt/local/lib/libml.dylib – Salida Software Nov 24 '10 at 18:32
6

In the newer version of Qt, this needs to be done to avoid a package not found error:

QT_CONFIG -= no-pkg-config
CONFIG += link_pkgconfig
PKGCONFIG += protobuf #or whatever package here

Also had to do this for Mac:

mac {
  PKG_CONFIG = /usr/local/bin/pkg-config
}
SteveEng
  • 331
  • 2
  • 6
  • Thanks. It works!! Is it better move QT_CONFIG, CONFIG, PKGCONFIG into mac { } section for cross compilation ? – Hongsoog Jun 10 '20 at 06:42
4

Something like this in your qmake file should do

LIBS += `pkg-config --libs opencv`

Edit: Hmm, Ken Bloom's answer might be more portable, but erhm not documented?

nicomen
  • 1,183
  • 7
  • 16
3

Add the following lines to your .pro file:

INCLUDEPATH += `pkg-config --cflags opencv`
LIBS += `pkg-config --libs opencv`
RawMean
  • 8,374
  • 6
  • 55
  • 82