C++ 使用 pkg-config 输出将库链接到 QT 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3517694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:01:13  来源:igfitidea点击:

Linking libraries to a QT project using pkg-config output

c++qtqmake

提问by MVG

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

这是一个有点菜鸟的问题。我正在尝试将 OpenCV 库添加到 QT 项目中。

This questionsays 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

更新:尝试了 Ken Bloom 的建议,但无法编译。实际生成的编译器命令是

# 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

回答by Ken Bloom

CONFIG += link_pkgconfig
PKGCONFIG += opencv

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

(我从http://beaufour.dk/blog/2008/02/using-pkgconfig.html得到了这个答案)

回答by Salida Software

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

肯的​​回答效果很好。我只需要先删除 += 两侧的空格。

CONFIG+=link_pkgconfig PKGCONFIG+=opencv

回答by nicomen

Something like this in your qmake file should do

在你的 qmake 文件中应该做这样的事情

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

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

编辑:嗯,Ken Bloom 的答案可能更便携,但 erhm 没有记录?

回答by SteveEng

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

在较新版本的 Qt 中,需要这样做以避免包未找到错误:

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

Also had to do this for Mac:

还必须为 Mac 执行此操作:

mac {
  PKG_CONFIG = /usr/local/bin/pkg-config
}

回答by RawMean

Add the following lines to your .pro file:

将以下行添加到您的 .pro 文件中:

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