2

I am new to Qt and openCV, and i try to make a simple project with code:

in the .pro:

QT       += core

QT       -= gui

QT  += widgets

TARGET = latihan_2
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

INCLUDEPATH += E:\\OpenCV\\OpenCV\\opencv\\build\\include

LIBS += E:\OpenCV\OpenCV\opencv\build\x86\vc10\lib\opencv_core246.lib
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\vc10\lib\opencv_highgui246.lib
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\vc10\lib\opencv_imgproc246.lib
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\vc10\lib\opencv_features2d246.lib
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\vc10\lib\opencv_calib3d246.lib

in the main.cpp:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(){
    //read image
    cv::Mat image;
    image = cv::imread("img.jpg");
    //create image window named "My image"
    cv::namedWindow("My Image");
    //show the image on window
    cv::imshow("My image", image);
    //wait key for 5000ms
    cv::waitKey(5000);
    return 1;

}

however, it always give error about the undefined reference to cv::imread, cv::namedWindows, and the other CV functions i used.

i use Qt creator 2.8.1, based on Qt 5.1.1, and openCV-2.4.6.0

Any help would be greatly appreciated! thanks

6
  • Which compiler do you use? "\x86\vc10" probably means that opencv is compiled with Visual C++. Do you use VC++ for project compilation too or use mingw32? Commented Nov 15, 2013 at 11:43
  • I just change it into : LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_core246.dll.a LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_highgui246.dll.a LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_imgproc246.dll.a LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_features2d246.dll.a LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_calib3d246.dll.a and it's working! Thank you old-ufo! Commented Nov 15, 2013 at 14:46
  • You are welcome. BTW, for your simple application only core and highgui libs are needed. Commented Nov 15, 2013 at 15:15
  • What exactly is a problem? Is it error on loading, it does not show the window, etc.? Commented Nov 16, 2013 at 1:33
  • please see here stackoverflow.com/questions/20013903/… Commented Nov 16, 2013 at 3:13

4 Answers 4

2

undefined reference errors are a linking problem, which means that your project compiled successfully but the linker is unable to find the binary code for those functions.

I have a very simple OpenCV/Qt project that is setup to be compiled on Windows/Linux/Mac OS X. If you take a look at the .pro file, you'll notice that for Windows I do:

win32 {
    message("* Using settings for Windows.")

    INCLUDEPATH += "C:\\opencv\\build\\include" \
                   "C:\\opencv\\build\\include\\opencv" \
                   "C:\\opencv\\build\\include\\opencv2"

    LIBS += -L"C:\\opencv\\build\\x86\\vc10\\lib" \
        -lopencv_core242 \
        -lopencv_highgui242 \
        -lopencv_imgproc242
}

Make sure to replace the 242 number referenced by LIBS with the specific OpenCV version you have.

It's also important to state that OpenCV is compiled with specific flags, and depending on the binary version you installed, sometimes you also need to add the following instructions to the .pro file of your project:

QMAKE_CXXFLAGS_DEBUG += -Zi -MTd
QMAKE_CXXFLAGS_RELEASE += -MT  

MTd refers to Multithreaded-Debug-DLL and MT stands for Multithreaded static linking.

Sign up to request clarification or add additional context in comments.

Comments

0

Google brought me here when I had the same problem. The solutions here didn't helped me. But finally I found the problem in my case: I didn't set a value for CMAKE_BUILD_TYPE in cmake gui. You have the choice between release and debug, and I think you must choose one.

I compiled OpenCV 3.0.0 successfully thanks to that last tip.

Comments

0

karlphillips is correct, there is an error in the way you are linking your opencv dependencies to Qt. You can manually enter the information as indicated by other answers here (every time I did it manually I ended up messing it all up) or you can you use the built in "Add Library..." option (SUPER EASY).

The steps listed below are found in the Qt5 documentation: [http://doc.qt.io/qtcreator/creator-project-qmake-libraries.html][1] under the "To Add Library" section.

  1. Right click on the project file located in the 'project pane' on the left side of the creator... and select "Add Library..."
  2. Follow the instructions of the wizard

Let me add some specificity from here...

  1. Select "External Library"
  2. For the "Library File" navigate to your opencv_worldXXX.lib file (or opencv_worldXXXd.lib file, you will notice that by specifying only one or the other the wizard has a checkbox which includes the other automatically) [ex. ...\opencv\build\x64\vc12\lib\opncv_world.lib]
  3. For the "Include Folder" navigate to the "include" folder within the build. [ex. ...\opencv\build\include]
  4. Select your operating system, dynamic/static library (whichever is appropriate)
  5. Hit NEXT, CLEAN UP, and RUN!

1 Comment

You look to be adding a very similar answer to multiple questions. When possible, it would be better to answer once on the most canonical question and flag the other questions as duplicates.
-1

You probably should not use ::cv, just directly use the function and try.

Sorry for the wrong suggestion, the real reason is not able to find your libs:

should use this: LIBS += E:\\OpenCV\\OpenCV\\opencv\\build\\x86\\vc10\\lib\\opencv_core246.lib

1 Comment

i've tried this. and the error is changed into 'Mat' was not declared in this scope, 'image' was not declared in this scope, etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.