Ask Your Question

[Ubuntu 18.04] how add opencv built from source to QT applications

asked 2019-01-15 04:55:41 -0500

mikeitexpert gravatar image

updated 2019-01-15 04:56:15 -0500

I have built opencv 4 from source and I would like to add it to my QT5 application.

source folder: /home/mike/opencv-4.0.1/ build folder: /home/mike/opencv-4.0.1/build/

I tried to define the below opencv.pri file to keep things organized but I get so many no such file or directory for included files in the source.

Any clue is much appreciate it.

here is the pri file:

INCLUDEPATH += -I/home/mike/opencv-4.0.1/include
LIBS += -L/home/mike/opencv-4.0.1/build -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_shape -lopencv_videoio -lopencv_calib3d -lopencv_face

and here is the pro file.

# Project created by QtCreator 2019-01-01T16:34:40

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets


TARGET = OpticalMeasure

QT += core gui xml xmlpatterns
QT += multimedia multimediawidgets
QT += concurrent widgets

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11


#INCLUDEPATH += /usr/include/opencv

#LIBS += -L/usr/lib/x86_64-linux-gnu/ -lopencv_core -lopencv_imgcodecs -lopencv_highgui
#LIBS += -L/usr/local/lib -lopencv_shape -lopencv_videoio
#-lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_shape -lopencv_videoio -lopencv_calib3d -lopencv_face
LIBS += -L/home/mike/Qt5.12.0/5.12.0/gcc_64/lib

        main.cpp \
        mainwindow.cpp \
    utils/settings.cpp \
    utils/utils.cpp \
    workerthread.cpp \

        mainwindow.h \
    utils/constants.h \
    utils/settings.h \
    utils/utils.h \

FORMS += \
        mainwindow.ui \
    videosettings.ui \

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

    resources.qrc \


    resources/calibration.xml \
    resources/calibration.xml \
    resources/calibration.json \
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2019-01-15 05:10:04 -0500

kbarni gravatar image

updated 2019-01-15 05:11:28 -0500

First, it's good to install the library. After make, do a sudo make install to copy the library files to the system folders (/usr/local/...)

Then, add to the QT .pro file:

# OpenCV
INCLUDEPATH += /usr/local/include/opencv4
LIBS += $(shell pkg-config opencv --libs)

That's all!

I think there are several problems with your pri file (the paths are not always correct). It's better to use the cleaner method described above.

edit flag offensive delete link more


@ can you please let me know where in the .pro file I should add the the two lines you mentioned? at the top ? in the middle ? thank you

mikeitexpert gravatar imagemikeitexpert ( 2019-01-15 13:34:12 -0500 )edit

It doesn't matter. I put it generally before the SOURCES/HEADERS part.

kbarni gravatar imagekbarni ( 2019-01-16 10:22:11 -0500 )edit

answered 2019-01-15 05:03:38 -0500

berak gravatar image

assuming you installed it correctly, for opencv4, you have to use:

INCLUDEPATH += -I/usr/local/include/opencv4


LIBS += -L/usr/local/lib

(yes, the include path was changed, recently, and it's admittedly not very intuitive)

edit flag offensive delete link more


thanks but can't I just use the source and build folders for include and libraries respectively?

mikeitexpert gravatar imagemikeitexpert ( 2019-01-15 06:22:24 -0500 )edit

no, you can't. neither the src/include nor the build/include folder contain the nessecary headers, look it up.

you can use the libs from the build folder though.

berak gravatar imageberak ( 2019-01-15 06:28:28 -0500 )edit

ok thanks but my includes getting ugly like : #include <opencv4/opencv2/opencv.hpp> Is it ok??

mikeitexpert gravatar imagemikeitexpert ( 2019-01-15 06:38:40 -0500 )edit

no, not ok. try NOT to deviate from the rules, please.

#include <opencv2/opencv.hpp>

(the opencv4 is already in the include path)

berak gravatar imageberak ( 2019-01-15 06:50:20 -0500 )edit

sorry my bad I had to restart the QT

mikeitexpert gravatar imagemikeitexpert ( 2019-01-15 06:56:45 -0500 )edit

@berak thanks for the comments ... let say I want to add more libraries to my build (eg. java bindings etc) how can I install extra those? can I just run sudo make install

mikeitexpert gravatar imagemikeitexpert ( 2019-01-15 13:29:43 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-01-15 04:55:41 -0500

Seen: 52 times

Last updated: Jan 15