Add opencv library to every qt project

本小妞迷上赌 提交于 2019-11-30 09:25:03

问题


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

my OS is ubuntu 16.04.3


回答1:


1) You can create a .prf (project feature) file in your mkspecs/features directory:

/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

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

...
CONFIG += ... opencv
...

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) Alternatively, if you don't want to pollute Qt installation, you can use manual include:

opencv.pri

INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and another libraries

myprogram.pro

include(path/to/opencv.pri)
...



回答2:


When you installed opencv you must also install the opencv.pc file, this file can be used to make it simple, since Qt supports package.config, if so, it replaces what it shows by the following:

unix: CONFIG += link_pkgconfig
unix: PKGCONFIG += opencv

Actually Qt Creator offers a simple way, you just have to right click on the name of your project and select the option Add Library:

Then you will get a dialog where you must select the type of library:

In this case I used the fourth option, and put the name of the library: opencv.

Then you press the next and finish buttons.



来源:https://stackoverflow.com/questions/47611221/add-opencv-library-to-every-qt-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!