PDA

View Full Version : [OpenCV] Linking problem



danioto
29th October 2012, 21:02
Hi,
I'll describe my problem with linking OpenCV 2.4.2 in Qt. I have build source OpenCV with two compilers MVSC 10 and MinGW and tried to link libraries with Qt.

1) First of all, I've tried to link libraries builded with MinGW (and cmake). But I found out, that there's no "*.lib" files only "*.dll.a". So in *.pro I've tried to link those "dll.a" files, but it don't work...


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/mybuildmingw/install/lib \
-llibopencv_highgui242


It shows "exited with code -1073741511", so it's definitely a dll linking problem. I've tried to write "-llibopencv_highgui242.a" and "-llibopencv_highgui242.dll" and "-llibopencv_highgui242.dll.a" but nothing is working...

2) Next, I tried to link libraries builded with MVSC 10. Here are "*.lib" files so it was good with code:


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

And program is working with code:


IplImage* img = cvLoadImage( "C:\\opencv.png" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);

But if I only want to use something connected with VideoCapture or Mat for example "cv::Mat I;" if gives me the compiling error:


c:\Programs\OpenCV-2.4.2\mybuildmingw\install\include\opencv2\core\ma t.hpp:278: błąd:undefined reference to `cv::fastFree(void*)'
c:\Programs\OpenCV-2.4.2\mybuildmingw\install\include\opencv2\core\ma t.hpp:367: błąd:undefined reference to `cv::Mat::deallocate()'
:-1: błąd:collect2: ld returned 1 exit status

Do you have any ideas how to solve this problem? I would really apreciate it, because I'm fighting with it whole week...

Robbie
29th October 2012, 22:19
Firstly, you never link .dll files. The first two letters stand for 'dynamically-linked'. This means that the linking happens at run-time, not compile-time.

Secondly, you must link static libraries compiled with the same compiler. That means that .a static libraries must be used if you're compiling with MinGW, and .lib files when compiling with MSVC.

Also, double-check that the classes, functions and variables that you're using from the 3rd-party library are part of their API. If they're not marked as export symbols, no code will be generated allowing you to load them from the .dll. By this, I mean that the classes, functions and variables must have __declspec(dllexport) and __declspec(dllimport) in their signatures (or macros defined as such).

If you don't know anything about the above, I suggest you read the topic Creating Shared Libraries from the Qt Assistant

ChrisW67
30th October 2012, 02:31
1) ... It shows "exited with code -1073741511", so it's definitely a dll linking problem.

As I explained in your other thread... this is a run time problem, not a compile/link problem. No amount of fiddling around with the PRO file will affect the run time environment that your application must find its dependent libraries in. If the program linked then it should be able to run if it can find the libraries it depends on at run time. A set of OpenCV DLLs built with a compiler compatible to the one you used for your executable must be present in the run time environment of your program.

If there are no matching OpenCV libraries in the run time environment then the program will fail to launch with the sort of message you describe.
If there are multiple sets of OpenCV libraries that might be found in the run time environment then there is the possibility of picking up a mismatched set generating the sort of message you describe.

If the program did not link, which is not the error message you gave us, then it will possibly be because the name of the link library is "opencv_highgui242" (MSVC) or "opencv_highgui242.dll" (MingW, yes libopencv_highgui242.dll.a is link library) not "libopencv_highgui242".



Here is my recommended course of action:

Remove any and all OpenCV directories from the system's PATH variable.
Locate or build a set of OpenCV libraries with the same compiler you are using for your program.
Remove all other copies of OpenCV from your machine
Clean and rebuild your application using Qt Creator
In Qt Creator click on the Projects tool bar icon
At the top of the panel select the Targets tab, and then the Run settings
Under Run Environment expand the Details and select Build Environment
In the environment that is listed locate PATH and double-click the value
To the end of the PATH value add the path to the OpenCV directory that contains the built DLL files.
Your application should now run correctly inside the Qt Creator environment. Until it does do not worry about making the program run outside this environment


With OpenCV installed into D:\OpenCV with MSVC the PRO file should contain:


INCLUDEPATH += d:/OpenCV/include
LIBS += -Ld:/OpenCV/lib -lopencv_core242 -lopencv_highgui242 ...

and your run time environment PATH should contain:


...;D:\OpenCV\bin

The library names are subtly different with MingW.

danioto
1st November 2012, 02:33
Thanks very much! I really tried to make it your way, but nothing was working. It was mayby because I'm a beginner in qt programming... But, I get other version of OpenCV: stable 2.4.0 and did everything like before and it worked! It appers that OpenCV-2.4.2 works fine with MVSC but not good with Qt (mingw) and getting stable version helped. My *.pro file looks now:



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

LIBS += -LC:/Programs/Programming/OpenCV-2.4.0/mybuildmingw/install/lib \
-llibopencv_calib3d240 \
-llibopencv_contrib240 \
-llibopencv_core240 \
-llibopencv_features2d240 \
-llibopencv_flann240 \
-llibopencv_gpu240 \
-llibopencv_highgui240 \
-llibopencv_imgproc240 \
-llibopencv_legacy240 \
-llibopencv_ml240 \
-llibopencv_nonfree240 \
-llibopencv_objdetect240 \
-llibopencv_photo240 \
-llibopencv_stitching240 \
-llibopencv_video240 \
-llibopencv_videostab240

Thank you very much for helping me :)