compilation error of OpenCV cpp code using Qt - c++

I am using a very basic OpenCV code to compile using Qt but every time the following errors are coming.
OpenCV version
OpenCV version : 3.4.3
Major version : 3
Minor version : 4
Subminor version : 3
My code
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int main()
{
Mat I = imread("path/filename.ppm", 1);
imshow("conv_image",I);
waitKey();
return 0;
}
.pro file
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you
use
# any feature of Qt which as been marked 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.
DEFINES += QT_DEPRECATED_WARNINGS
# 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
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -
lopencv_highgui
Given Error
:-1: warning: libopencv_core.so.3.4, needed by /usr/local/lib/libopencv_imgcodecs.so, may conflict with libopencv_core.so.2.4
:-1: error: main.o: undefined reference to symbol '_ZN2cv6String10deallocateEv'
/usr/local/lib/libopencv_core.so.3.4:-1: error: error adding symbols: DSO missing from command line
:-1: error: collect2: error: ld returned 1 exit status
In my .pro file, I have used the following line(from the different link I have found 3 ways, I have tried all but failed)
First
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -
lopencv_highgui
Second
INCLUDEPATH +=/usr/local/include/opencv
LIBS +=`pkg-config --libs opencv`
Third
I have used information from here
Please give me suggestion to solve the issue.

Related

Qt 5.14.2 does not file .so file when building

I struggle for cross compiling for Raspberry pi 4. My OS is Ubuntu 20.04. I builded cross compile platform and I have been run simple quick application. (I created project on PC and I could see output window on Raspberry pi screen).
I want run more big project on Raspberry pi4 with cross compile. I want edit code on my PC and I want see output on Raspberry pi 4. I have been worked this project on Raspberry pi 4 without Qt.
I tried run qmake for .pro file and I compiled the resulting makefile file. It's working. I want do same process with cross compile but I have errors this place. Qt does not find some .so files but I checked and this files are exist on the specified path. I tried add external library,I tried add external library and I tried editing path of LIBS and INCLUDEPATH but result did not change.
P.S. I used this commands when I building cross compile. https://github.com/PhysicsX/QTonRaspberryPi/blob/main/qt5.14.2onRaspberrypi
My .pro file is:
QT += quick widgets serialport multimedia
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
INCLUDEPATH += -I/opt/qt5pi/sysroot/usr/include/opencv2
LIBS += -L/opt/qt5pi/sysroot/usr/lib -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_videostab -lopencv_features2d
# You can also make your code fail to compile if it uses 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
SOURCES += \
main.cpp \
file.cpp \
gcodesender.cpp \
movementmanager.cpp \
usbmanager.cpp \
imageresizer.cpp \
serialcommunicator.cpp \
frameprocessor.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /home/pi/dev/$${TARGET}/
!isEmpty(target.path): INSTALLS += target
HEADERS += \
file.h \
gcodesender.h \
movementmanager.h \
usbmanager.h \
imageresizer.h \
serialcommunicator.h \
frameprocessor.h
DISTFILES += \
theme/DarkTheme_copy.qml \
theme/WhiteTheme.qml
Part of the error is below:
linux-gnueabihf/libavformat.so.58, not found (try using -rpath or -rpath-link)
/opt/qt5pi/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld: warning: libvcos.so, needed by /opt/qt5pi/sysroot/usr/lib/arm-linux-gnueabihf/libavformat.so.58, not found (try using -rpath or -rpath-link)
/opt/qt5pi/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld: warning: libblas.so.3, needed by /opt/qt5pi/sysroot/lib/libarmadillo.so.9, not found (try using -rpath or -rpath-link)
/opt/qt5pi/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld: warning: liblapack.so.3, needed by /opt/qt5pi/sysroot/lib/libarmadillo.so.9, not found (try using -rpath or -rpath-link)
/opt/qt5pi/sysroot/lib/libarmadillo.so.9: undefined reference to `zgemm_'
/opt/qt5pi/sysroot/usr/lib/arm-linux-gnueabihf/libarpack.so.2: undefined reference to `clahqr_'
/opt/qt5pi/sysroot/lib/libarmadillo.so.9: undefined reference to `sdot_'
/opt/qt5pi/sysroot/usr/lib/arm-linux-gnueabihf/libavcodec.so.58: undefined reference to `vcsm_malloc_cache'
/opt/qt5pi/sysroot/lib/libarmadillo.so.9: undefined reference to `dgehrd_'
/opt/qt5pi/sysroot/lib/libarmadillo.so.9: undefined reference to `zgetrf_'
/opt/qt5pi/sysroot/lib/libarmadillo.so.9: undefined reference to `zggev_'
And my compiled output all errors(I'm stuck on the character limit ): https://pastebin.pl/view/cbeede8a
How can I fix error ?
Thanks

Linking OpenCV to a shared library "mylib", `undefined reference to ...` errors

I am trying to use OpenCV-2.4.13 shared objects (libopencv_core.so,libopencv_imgproc.so etc.) in mylib.so.
These OpenCV shared objects are compiled and installed with standard build procedure (make, make install) and I confirmed that they are located in /usr/local/lib, they can be used directly with an application without any problem.
I am using Qt 5.8 on Ubuntu 16.04 for building process.
In mylib.so, I use common cv::Mat functions. Looking at the similar questions, I have already skimmed this article, it suggests either using -rpath or -rpath-link options during linking mylib.so with an application. I also did read this article from Qt and applied the suggested method when building mylib.so: adding a a_global.h and defining an export symbol as Q_DECL_EXPORT.
I am providing .pro file for mylib.so below.
QT -= gui
CONFIG += c++11
TARGET = mylib
TEMPLATE = lib
DEFINES += MYLIB_LIBRARY
SOURCES += a.cpp
HEADERS += a.h external_libraries.h a_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
INCLUDEPATH += "/usr/local/include"
LIBS += -L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core\
-lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgproc\
-lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_ocl\
-lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_video\
-lopencv_videostab
Also, here is the .pro file for a dummy test application.
QT += core
QT -= gui
CONFIG += c++11
TARGET = apptest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += "/home/foo/mylib/" #this is where `a.h` is
LIBS += "/home/foo/mylib/build/mylib.so.1.0.0"
QMAKE_CXXFLAGS += "-Wall"
QMAKE_LFLAGS += "-Wl,-rpath-link,/usr/local/lib"
QMAKE_LFLAGS += "-Wl,--unresolved-symbols=ignore-in-shared-libs"
You can see that I am providing /usr/local/lib as the -rpath-link where OpenCV shared objects reside. I am also instructing the linker to ignore unresolved symbols from shared libraries, which is suggested in this question and documented here.
The "right way" according to many sources is not to link OpenCV (or any dependency) in application, because this causes overlinking. In apptest, when I don't link OpenCV, I get the following linker error:
undefined reference to cv::Mat::deallocate()
undefined reference to cv::fastFree(void)
collect2: error: ld returned 1 exit status
Looking at the error, my intuition is, somehow -rpath-link is not doing what it is supposed to do.
I was not able to resolve this, I decided to deliberately link OpenCV with apptest just to see what happens. What happens is awkward: it compiles and links without errors (whether I provide --unresolved-symbols=ignore-in-shared-libs option or not). But when I run the app, it crashes without any exceptions from OpenCV; cv::imread operation does not work as expected, rows and cols of image is zero.
I checked both apptest and mylib.so with readelf -d. Nothing strange occured to me,
mylib.so depends on libQt5Core and libopencv_* and it has the right SONAME entry.
apptest depends on mylib.so,libQt5Core and also libopencv_* when I did the deliberate linking.
I also tried to debug apptest with gdb. Output was not very helpful...
(gdb) r
Starting program:
/home/foo/apptest/build/apptest
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00007fffffffc9b0 in ?? ()
I assume linking is not done properly, but why? How can I resolve this without having to link OpenCV libs directly in app?

cv::VideoCapture from string gives linker error in Qt5 on Mac

Problem
I am trying to get the OpenCV VideoCapture class running in a Qt project.
When I call it with an int (0 for the video camera on my mac), it works fine:
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
...
cv::VideoCapture cap(0);
The project compiles and when I run it, the light on my camera goes on.
BUT: when I try to compile it with a std::string as argument, I get a linker error:
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
...
cv::VideoCapture cap("/Users/xxxxx/small.mp4");
results in:
Undefined symbols for architecture x86_64:
"cv::VideoCapture::VideoCapture(std::string const&)", referenced from:
MainWindow::MainWindow(QWidget*) in mainwindow.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
althought the constructor from std::string should actually exist:
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-videocapture
Settings
My specifications:
Qt 5.4.0 (Clang 6.0 (Apple), 64 bit)
Mac OSX 10.10.1
OpenCV 2.4.9 installed from homebrew
My .pro file:
QT += core gui widgets
TARGET = VideoCaptureTest
TEMPLATE = app
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += /usr/local/include
LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_ml
LIBS += -lopencv_video
LIBS += -lopencv_videostab
LIBS += -lopencv_features2d
LIBS += -lopencv_calib3d
LIBS += -lopencv_objdetect
LIBS += -lopencv_contrib
LIBS += -lopencv_legacy
LIBS += -lopencv_flann
What I tried
1.
I found this question on SO, which seems to be the exact same problem as mine:
Linking error: undefined reference to `cv::VideoCapture::open(std::string const&)`
It seem my problem could have something to do with clashing versions of OpenCV. But I checked my include directories and my lib directories and I seem to only have 2.4.9 installed. Is there maybe a different version that comes with Qt?
I tried including version 2.4.9 directly in my .pro file with
LIBS += -lopencv_core2.4.9
...
But how can I make sure I include this version as well in my source file?
2.
I though it might have something to do with Mac and x86_64 stuff. I included the following lines, which I found somewhere online, in my .pro file:
CONFIG += x86 ppc x86_64 ppc64
CONFIG += MAC_CONFIG
MAC_CONFIG {
QMAKE_CXXFLAGS = -std=c++11 -stdlib=libstdc++ -mmacosx-version-min=10.7
QMAKE_LFLAGS = -std=c++11 -stdlib=libstdc++ -mmacosx-version-min=10.7
}
But this also showed no effect.
3.
Here (http://answers.opencv.org/question/14772/solved-linking-error-undefined-reference-to-cvvideocaptureopenstdstring-const/) it is suggested to try cv::String instead of std::string. I also tried this:
#include "opencv2/opencv.hpp"
...
cv::String s("/Users/xxxxx/small.mp4");
cv::VideoCapture cap(s);
and strangely I still get the exact same error:
Undefined symbols for architecture x86_64:
"cv::VideoCapture::VideoCapture(std::string const&)", referenced from:
MainWindow::MainWindow(QWidget*) in mainwindow.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
which seems like the cv::String is actually converted to std::string, I guess because the compiler somewhere finds the definition of the constructor with std::string, but the linker does not?
4.
Apparently there was a similar problem with OSX 10.9, not with cv::VideoCapture, but in general resulting also in linking error for x86_64. The solution was to include
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9
in the .pro file. I tried this as well with 10.9 and 10.10 (which I am actually using). Still the same error. Could this be a problem with my version of OSX?
Help
I found no other suggestions on how to solve the problem. Does somebody have an idea what I am doing wrong? Any help appreciated!
Patrick
The answer has been given in the discussion(#wezside) of the link (item 1 of your description).
for OpenCV 3.0 (also 2.4.9 ?)
you need to add
LIBS += -lopencv_videoio
to the .pro, which is missing in yours.
After researching for several hours, I found that adding QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.11 to your .pro file and removing the previous .pro.user file and build directory can solve the problem. The second step is required.
I'm on El Capitan.

SDL2 undefined reference sdl_Main in QtCreator

Im trying to use QtCreator to create a project including SDL2. But SDL is failing to compile correctly and I can't see the error.
My QtCreator .pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Psiora
TEMPLATE = app
SOURCES += main.cpp\
ui_mainwindow.cpp
HEADERS += ui_mainwindow.h
FORMS += ui_mainwindow.ui
INCLUDEPATH += "D:\devel\SDL2-2.0.3\i686-w64-mingw32\include\SDL2"
LIBS += -L"D:\devel\SDL2-2.0.3\i686-w64-mingw32\lib" \
-lmingw32 \
-mwindows \
-lSDL2main \
-lSDL2
Main.cpp
#include "ui_mainwindow.h"
#include <QApplication>
#define SDL_MAIN_HANDLED //Let QT Supply WinMain
#include "SDL.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
UI_mainWindow win;
win.show();
return app.exec();
}
Compiler errors
g++ -Wl,-subsystem,windows -mthreads -o debug\Psiora.exe debug/main.o debug/ui_mainwindow.o debug/moc_ui_mainwindow.o -lglu32 -lopengl32 -lgdi32 -luser32 -lqtmaind -LD:\devel\SDL2-2.0.3\i686-w64-mingw32\lib -lmingw32 -mwindows -lSDL2main -lSDL2 -LD:\devel\Qt\5.2.1\mingw48_32\lib -lQt5Widgetsd -lQt5Guid -lQt5Cored
D:\devel\SDL2-2.0.3\i686-w64-mingw32\lib/libSDL2main.a(SDL_windows_main.o): In function `console_main':
/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/SDL_windows_main.c:140: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status
I don't understand why SDL is giving an undefined reference to sdl_Main, when it has been linked in the correct order. I also don't get where this "/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/" has come from when there is no such path on my computer, nor have I added the path myself.
My understanding is the SDL_Main provides WinMain however I wish to use the WinMain provided by Qt instead so I have defined the SDL_MAIN_HANDLED define to stop SDL using SDL_Main.
Addition Information:
OS: Windows 8.1 Pro 32bit
Compiler: MinGW 4.8 - The default one provided with the Qt Installer
Qt: 5.2.1
SDL 2.0.3 - Downloaded directly from libsdl.org, unmodified with no patches.
Add this to your project file to solve this:
win32:QMAKE_LIBS_QT_ENGTRY -= -lqtmain
win32-g++:DEFINES -= QT_NEEDS_QMAIN
or
CONFIG-= windows
QMAKE_LFLAGS += $$QMAKE_LFLAGS_WINDOWS

Qt C++ ffmpeg cannot find library

I use Qt 5.0.2 with Mingw 4.7 on windows.
I'm working on a c++ program and i'm trying to get the duration of video files. I found ffmpeg. Now i tried to compile a simple program but I think it fails with the libraries. I've tried to include both the shared and dev but they both will fail. ( Zeranoe FFmpeg builds)
This is my pro file:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH += C:\programming\ffmpeg-20130606-git--win32-shared\bin
LIBS += -LC:\programming\ffmpeg-20130606-git--win32-shared\bin -lavcodec-55.dll - lavformat-55.dll -lavutil-52.dll
i get errors like:
":-1: error: cannot find -lavcodec-55.dll"
":-1: error: cannot find -lavformat-55.dll"
I've also tried:
INCLUDEPATH += C:\programming\ffmpeg-20130606-git--win32-dev\include
INCLUDEPATH += C:\programming\ffmpeg-20130606-git--win32-dev\lib
LIBS += -LC:\programming\ffmpeg-20130606-git--win32-dev\lib -lavcodec -lavformat -lavutil
LIBS += -LC:\programming\ffmpeg-20130606-git--win32-dev\lib -llibavcodec.dll.a -llibavformat.dll.a -llibavutil.dll.a
Here it gives a error "During startup program exited with code 0x0000135'
I even tried to include each library individually. But no results..
This is my main file:
#include <iostream>
using namespace std;
extern "C" {
#include <libavcodec/avcodec.h> // required headers
#include <libavformat/avformat.h>
}
int main(int argc, char**argv) {
av_register_all(); // offending library call
return 0;
}
A program simple as this will have 2 outcomes:
the program runs but crashes if i call 'av_register_all()'
It just tells me he cannot find the libraries.
Can someone tell me what it is i'm doing wrong? Or even give me a hint? I can't really find a lot of good documentation on this one.
Thanks in advance!
Configure PRO-file as shown below:
INCLUDEPATH += C:\programming\ffmpeg-20130606-git--win32-dev\include
LIBS += -LC:\programming\ffmpeg-20130606-git--win32-dev\lib
LIBS += -lavcodec -lavformat -lavutil
Then, build the project, copy the all dll libraries from C:\programming\ffmpeg-20130606-git--win32-shared\bin to exe output path and run application.
UPD
Maybe you need to copy some of the following libraries from Qt\5.0.2\mingw47_32\bin\:
libgcc_s_sjlj-1.dll
libstdc++-6.dll
libwinpthread-1.dll