Results 1 to 4 of 4

Thread: [OpenCV] Linking problem

  1. #1
    Join Date
    Oct 2012
    Location
    Warsaw,Poland
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [OpenCV] Linking problem

    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...
    Qt Code:
    1. INCLUDEPATH += C:/Programs/OpenCV-2.4.2/mybuildmingw/install/include
    2. INCLUDEPATH += C:/Programs/OpenCV-2.4.2/mybuildmingw/install/include/opencv
    3.  
    4. LIBS += -LC:/Programs/OpenCV-2.4.2/mybuildmingw/install/lib \
    5. -llibopencv_highgui242
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. INCLUDEPATH += C:/Programs/OpenCV-2.4.2/mybuildmingw/install/include
    2. INCLUDEPATH += C:/Programs/OpenCV-2.4.2/mybuildmingw/install/include/opencv
    3.  
    4. LIBS += -LC:/Programs/OpenCV-2.4.2/mybuild/lib/Debug \
    5. -lopencv_highgui242d
    To copy to clipboard, switch view to plain text mode 

    And program is working with code:
    Qt Code:
    1. IplImage* img = cvLoadImage( "C:\\opencv.png" );
    2. cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    3. cvShowImage("Example1", img);
    4. cvWaitKey(0);
    5. cvReleaseImage(&img);
    To copy to clipboard, switch view to plain text mode 

    But if I only want to use something connected with VideoCapture or Mat for example "cv::Mat I;" if gives me the compiling error:
    Qt Code:
    1. c:\Programs\OpenCV-2.4.2\mybuildmingw\install\include\opencv2\core\mat.hpp:278: błąd:undefined reference to `cv::fastFree(void*)'
    2. c:\Programs\OpenCV-2.4.2\mybuildmingw\install\include\opencv2\core\mat.hpp:367: błąd:undefined reference to `cv::Mat::deallocate()'
    3. :-1: błąd:collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jun 2010
    Location
    Pretoria, South Africa
    Posts
    22
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [OpenCV] Linking problem

    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

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: [OpenCV] Linking problem

    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:
    Qt Code:
    1. INCLUDEPATH += d:/OpenCV/include
    2. LIBS += -Ld:/OpenCV/lib -lopencv_core242 -lopencv_highgui242 ...
    To copy to clipboard, switch view to plain text mode 
    and your run time environment PATH should contain:
    Qt Code:
    1. ...;D:\OpenCV\bin
    To copy to clipboard, switch view to plain text mode 
    The library names are subtly different with MingW.
    Last edited by ChrisW67; 30th October 2012 at 01:32. Reason: spelling corrections

  4. #4
    Join Date
    Oct 2012
    Location
    Warsaw,Poland
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [OpenCV] Linking problem

    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:

    Qt Code:
    1. INCLUDEPATH += C:/Programs/Programming/OpenCV-2.4.0/mybuildmingw/install/include
    2. INCLUDEPATH += C:/Programs/Programming/OpenCV-2.4.0/mybuildmingw/install/include/opencv
    3.  
    4. LIBS += -LC:/Programs/Programming/OpenCV-2.4.0/mybuildmingw/install/lib \
    5. -llibopencv_calib3d240 \
    6. -llibopencv_contrib240 \
    7. -llibopencv_core240 \
    8. -llibopencv_features2d240 \
    9. -llibopencv_flann240 \
    10. -llibopencv_gpu240 \
    11. -llibopencv_highgui240 \
    12. -llibopencv_imgproc240 \
    13. -llibopencv_legacy240 \
    14. -llibopencv_ml240 \
    15. -llibopencv_nonfree240 \
    16. -llibopencv_objdetect240 \
    17. -llibopencv_photo240 \
    18. -llibopencv_stitching240 \
    19. -llibopencv_video240 \
    20. -llibopencv_videostab240
    To copy to clipboard, switch view to plain text mode 

    Thank you very much for helping me

Similar Threads

  1. Linking Error Qt OpenCV
    By Songi in forum Installation and Deployment
    Replies: 4
    Last Post: 28th August 2011, 04:03
  2. Linking NokiaQT with OpenCV
    By tan88122 in forum Newbie
    Replies: 1
    Last Post: 14th March 2011, 07:16
  3. OpenCv Problem
    By danics in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2010, 10:40
  4. Problem wth Opencv & Qt 4
    By Casper2004 in forum Qt Programming
    Replies: 0
    Last Post: 28th September 2009, 09:33
  5. I've got a qt&opencv problem.
    By eralvc in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd October 2008, 09:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.