MVG
MVG

Reputation: 343

Linking libraries to a QT project using pkg-config output

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

Upvotes: 31

Views: 38824

Answers (5)

Salida Software
Salida Software

Reputation: 446

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

CONFIG+=link_pkgconfig PKGCONFIG+=opencv

Upvotes: 10

SteveEng
SteveEng

Reputation: 331

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
}

Upvotes: 6

RawMean
RawMean

Reputation: 8717

Add the following lines to your .pro file:

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

Upvotes: 3

nicomen
nicomen

Reputation: 1203

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?

Upvotes: 4

Ken Bloom
Ken Bloom

Reputation: 58770

CONFIG += link_pkgconfig
PKGCONFIG += opencv

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

Upvotes: 45

Related Questions