PDA

View Full Version : Using external dll in Qt [OpenCV]



danioto
20th October 2012, 13:14
Hello!

I have a problem with integrating QtCreator with OpenCV. I've build the OpenCV correctly, source path is:
C:\Programs\OpenCV-2.4.2\opencv
and build path is:
C:\Programs\OpenCV-2.4.2\mybuild

First of all, I wanted to use the video capture, so I add in *.pro

INCLUDEPATH += C:/Programs/OpenCV-2.4.2/opencv/build/include
LIBS += -LC:/Programs/OpenCV-2.4.2/mybuild/bin/Debug/ -lopencv_highgui242d
LIBS += -LC:/Programs/OpenCV-2.4.2/mybuild/lib/Debug/ -lopencv_highgui242d

And something went wrong, because highgui.hpp was added corectly to main heder file, but when I try to use:

cv::VideoCapture capWebCam;
Qt said:

undefined reference to `cv::VideoCapture::VideoCapture()'

How can I fix it?

ChrisW67
21st October 2012, 03:20
Qt didn't say that. Your linker did. You fix it by linking the OpenCV library that contains cv::VideoCapture, which ever that is. It may be you have the right library but wrong library search path so look at all the relevant linker output for mentions of unfound libraries etc.

danioto
21st October 2012, 18:57
But, it's happening even if I add all libraries found in "lib/Debug/"...

danioto
28th October 2012, 23:07
Hello again!

After hours of battle with Qt and OpenCV and thousand of web sites I've found the solution to the previous problem. It appers, that my Qt compiler is MinGW and I build OpenCV with MVSC+ and I think it was the couse of problem.

But now I have another problem. I have only one line:

cv::VideoCapture capWebCam;

And after building it everything is fine, but afer run I got massage:

exited with code -1073741511

If I go to the project folder and try to execute the program, this one in release folder shows "Program stopped working", but this one from debug folder works fine (but don't do enything, because in constructor is only the line above).

I've read that -1073741511 code is connected with dll linking. But in folder of builded binary OpenCV-2.4.2 files is folder "bin", where are all *.dll's, and that's what I link to the program with:


INCLUDEPATH += C:/Programs/OpenCV-2.4.2/mybuildmingw/install/include

LIBS += -LC:/Programs/OpenCV-2.4.2/mybuildmingw/install/bin -llibopencv_highgui242
LIBS += -LC:/Programs/OpenCV-2.4.2/mybuildmingw/install/lib -llibopencv_highgui242

So, what do I do wrong? Can someone help me? :)

ChrisW67
28th October 2012, 23:46
The contents of the PRO file, INCLUDEPATH and LIBS, are to do with compiling and linking your code in to the final executable. Once compiled this file plays no part.

At run time your program must be able to locate the OpenCV DLLs in one of the usual places that Windows look is given the environment the program is run in. These places are generally: the same directory as the executable, somewhere in the directories listed in the PATH environment variable, or the Windows folders.

If you are running from an IDE you can use the IDE to set the run time environment the program is launched in. In Qt Creator: Projects, Targets, Desktop/Run, Run Environment.

If you are running outside the IDE then you should place the OpenCV directory into the system (or user) PATH.

danioto
29th October 2012, 00:19
OpenCV DLL's are placed only in PATH environment variable, I mean:

C:\Programs\OpenCV-2.4.2\mybuildmingw\install\bin
I added this line too:

C:\Programs\OpenCV-2.4.2\mybuildmingw\install\lib
Where "mybuildmingw" is folder with complied OpenCV files with MinGW.

And in *.pro file I have:

INCLUDEPATH += C:/Programy/OpenCV-2.4.2/mybuildmingw/install/include

LIBS += -LC:/Programy/OpenCV-2.4.2/mybuildmingw/install/bin -llibopencv_highgui242
LIBS += -LC:/Programy/OpenCV-2.4.2/mybuildmingw/install/lib -llibopencv_highgui242

So, that was what are you talking about?

================================================== =========================
EDIT:
I've solved the problem, but it's the most weirdest thing I've ever meet in programming... I have nothing connected with OpenCV in PATH environment variable and my *.pro file is:


INCLUDEPATH += C:/Programs/OpenCV-2.4.2/mybuildmingw/install/include
INCLUDEPATH += C:/Programs/OpenCV-2.4.2/mybuildmingw/install/include/opencv

LIBS += -LC:/Programs/OpenCV-2.4.2/mybuild/lib/Debug -lopencv_highgui242d \
-lopencv_core242d

So: "mybuildmingw" is folder with compiled OpenCV files with MinGW and "mybuild" is folder with compiled OpenCV files with MVSC+... Include files Qt takes from MinGW build and lib's from MVSC+ build. It's so weird, but it's working!



#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
...
IplImage* img = cvLoadImage( "C:\\opencv.png" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
cvWaitKey(0);

================================================== =========================
EDIT2:

It's not so simple as I thought. I have another linker problem:




#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
...
IplImage* img = cvLoadImage( "C:\\opencv.png" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
cvWaitKey(0);

cv::VideoCapture capture(0);

And *.pro:


INCLUDEPATH += C:/Programs/OpenCV-2.4.2/mybuildmingw/install/include
INCLUDEPATH += C:/Programs/OpenCV-2.4.2/mybuildmingw/install/include/opencv

LIBS += -LC:/Programs/OpenCV-2.4.2/mybuild/lib/Debug \
-lopencv_calib3d242d \
-lopencv_contrib242d \
-lopencv_core242d \
-lopencv_features2d242d \
-lopencv_flann242d \
-lopencv_gpu242d \
-lopencv_haartraining_engined \
-lopencv_highgui242d \
-lopencv_imgproc242d \
-lopencv_legacy242d \
-lopencv_ml242d \
-lopencv_nonfree242d \
-lopencv_objdetect242d \
-lopencv_photo242d \
-lopencv_stitching242d \
-lopencv_ts242d \
-lopencv_video242d \
-lopencv_videostab242d

And error occurs:


(...) undefined reference to `cv::VideoCapture::VideoCapture(int)'
(...) undefined reference to `cv::VideoCapture::~VideoCapture()'
collect2: ld returned 1 exit status

Any ideas?