将opencv库添加到每个qt项目 [英] Add opencv library to every qt project
问题描述
假设我们想将OpenCV库与Qt 链接,通常,我们在qmake(.pro文件)中添加INCLUDEPATH
和LIBS
,但是如果您是机器视觉工程师,那么大多数您的项目中必须包含OpenCV库,因此有什么方法可以在创建项目时添加opencv库.
我每次使用以下命令为我的项目添加OpenCV库.
suppose that we want to link OpenCV library with Qt,in common, we add INCLUDEPATH
and LIBS
in qmake(.pro file) but if you are a machine vision engineer then most of your projects have to include OpenCV library, so is there any way to add opencv library in time of creating project.
I use below command to add OpenCV library for my projects every time.
INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and another libraries
我的操作系统是 ubuntu 16.04.3
推荐答案
1)您可以在mkspecs/features
目录中创建.prf(项目功能)文件:
1) You can create a .prf (project feature) file in your mkspecs/features
directory:
/usr/share/qt5/mkspecs/features/opencv.prf
/usr/share/qt5/mkspecs/features/opencv.prf
INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and another libraries
现在只需将CONFIG += opencv
添加到您的.pro文件即可使其正常工作.或者,您甚至可以通过编辑mkspecs/qconfig.pri
:
Now simply add CONFIG += opencv
to your .pro file to have it working. Or you can even auto-enable this feature by editing mkspecs/qconfig.pri
:
/usr/share/qt5/mkspecs/qconfig.pri
/usr/share/qt5/mkspecs/qconfig.pri
...
CONFIG += ... opencv
...
顺便说一句. qconfig.pri
是qt_config
的一部分,所有QMake的机器相关规格均已加载qt_config
,因此它应始终有效.但是,也可以仅修补特定规范(例如/usr/share/qt5/mkspecs/linux-g++/qmake.conf
或适合您的配置的任何补丁).当然,甚至可以将所有这些INCLUDEPATH+=...
和LIBS+=...
直接添加到该qmake.conf
中,并完全摆脱.prf文件.
BTW. qconfig.pri
is a part of qt_config
, which is loaded by all QMake's machine-dependent specs, so it should always work. However, it's also possible to patch only a specific spec (for example, /usr/share/qt5/mkspecs/linux-g++/qmake.conf
, or whatever is appropriate for your configuration). Of course, it's even possible to add all these INCLUDEPATH+=...
and LIBS+=...
straight into that qmake.conf
and get rid of the .prf file completely.
2)或者,如果您不想污染Qt安装,则可以使用手动包括:
2) Alternatively, if you don't want to pollute Qt installation, you can use manual include:
opencv.pri
opencv.pri
INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and another libraries
myprogram.pro
myprogram.pro
include(path/to/opencv.pri)
...
这篇关于将opencv库添加到每个qt项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!