undefined reference qt opencv - qt

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

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.

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.

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.
Right click on the project file located in the 'project pane' on the left side of the creator... and select "Add Library..."
Follow the instructions of the wizard
Let me add some specificity from here...
Select "External Library"
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]
For the "Include Folder" navigate to the "include" folder within the build. [ex. ...\opencv\build\include]
Select your operating system, dynamic/static library (whichever is appropriate)
Hit NEXT, CLEAN UP, and RUN!

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

Related

Qt creator does not detect opencv

I am trying to run a program in Qt5.6 with openCV3.1, but no matter what I do, it does not detect openCV include.
Projects -> Build Environment -> Include :
I added C:\opencv\opencv3.1.0\opencv\build\include
Projects -> Build Environment -> LIB:
I added C:\opencv\opencv3.1.0\opencv\build\x64\vc14\lib
However, still in the code there is a yellow line indicating that it doesn't detect opencv:
and there are lots of errors like this:
The environment variable for openCV is also set as:
C:\opencv\opencv3.1.0\opencv\build
How should I install Qt with openCV to avoid these problems?
In the .pro file, add the following lines:
INCLUDEPATH += C:\opencv\opencv3.1.0\opencv\build\include
LIBS += -LC:\opencv\opencv3.1.0\opencv\build\x64\vc14\lib \
opencv310.lib \
And also you need to add the .dll file's path C:\opencv\opencv3.1.0\opencv\build\x64\vc14\bin to the system path (this time you need to restart the Qt IDE)

INCLUDEPATH in qmake project file doesn't work

I've got a problem with include in a qmake project. In my .pro file I've got:
INCLUDEPATH += "C:\OpenCV\build\include"
and in my cpp :
#include <opencv\cv.h>
The compiler indicates an error:
Cannot open include file: 'opencv\cv.h': No such file or directory
but if I write this in my cpp:
#include "C:\OpenCV\build\include\opencv\cv.h"
it works!
I build the project from within Qt Creator. What am I doing wrong?
You have to run qmake(build->run qmake) to validate changes in the pro file.
Qt creator Adding external library (still: Cannot open include file: 'GL/glew.h')
Your problem may be related to the fact that having backslashes in naked #include directives is undefined behavior.
Do the following.
Replace your include with
#include <opencv/cv.h>
Note the forward slash!
Remove the shadow build directory that Qt Creator has made for you. You will find it above the project directory, its name begins with build-.
Rebuild the project.
Note that this takes care of rerunning qmake.
here's one of my pro files:
# Blah Application
TEMPLATE = app
CONFIG += qt console staticlib debug_and_release
QT -= gui
QT += network sql xml
TARGET = blah
CONFIG(debug, debug|release){
DESTDIR = debug
LIBS += -L../../../lib/core/debug -lcore
} else {
DESTDIR = release
LIBS += -L../../../lib/core/release -lcore
}
DEPENDPATH += . ../../lib ../../../lib/core
INCLUDEPATH += . ../../lib ../../../lib/core
# Library files
HEADERS += mtBlahRTP.h
SOURCES += mtBlahRTP.cpp
# Input
HEADERS +=
SOURCES += main.cpp
The include path points to the RELATIVE directory of my lib files. mtBlahRTP.h and mtBlahRTP.cpp are in ../../lib
I have the same question, before building or running, you should qmake(Build=>qmake) it.
My configurations for INCLUDEPATH:
INCLUDEPATH+=D:\opencv\opencv\build\include
INCLUDEPATH+=D:\opencv\opencv\build\include\opencv
INCLUDEPATH+=D:\opencv\opencv\build\include\opencv2
I ran into a similar issue and what I found is that the QtCreator IDE is not re-reading the results of qmake and updating the "Cannot open" message. You need to close the offending file and re-open it - then you'll see that it no longer displays the error.
I had to do two steps: (re-)run qmake and rebuild the whole project - only then the INCLUDEPATH setting was considered correctly. (With QtCreator 3.5 and 3.6 (Qt 5.5 and Qt 5.6) on Windows.)
The only problem you are making is incorrectly linking the OpenCV library. The other answers given here may or may not work, but I have posted on another thread a surefire way to solve this problem using the "Add Library" wizard inside Qt Creator: https://stackoverflow.com/a/51914928/10245006
I was getting the error:
canserialcomm.o: In function `CanSerialComm::CanSerialComm()':
canserialcomm.cpp:(.text+0xc1): undefined reference to `vtable for CanSerialComm'
It turns out that the cause was it wasn't able to find canserialcomm.h where that constructor is declared. This was despite me having INCLUDEPATH in the project file point to the directory containing that header file:
INCLUDEPATH += . \
..
What I had to do to fix this is explicitely specify the header file; I added:
HEADER += ../canserialcomm.h
You should use double backslashes when in windows for qt creator with msvc. like this:
INCLUDEPATH += C:\\libcurl\\libcurl-vc-x64-release-dll-ipv6-sspi-winssl\\include
this will fix the problem.
Under windows you have to eliminate the -I before each directory that is added into the INCLUDEPATH variable.
For example:
Under windows:
INCLUDEPATH += "C:\lib\boost_1_61_0" (back-slash)
Under linux & mac:
INCLUDEPATH += -I"$$(HOME)/lib/boost_1_61_0" (note the -I and forward-slash)
I'm not sure whether it depends on different qmake version or not. But after finishing qmake command, I check the Makefile and the double -I is the issue.
You need to do several things. Fist, in the .pro file, you need quotation marks two backslashes at a time, like this:
INCLUDEPATH += "C:\\OpenCV\\build\\include\\opencv\\cv.h"
You alse need a frontslash in the #include in your .cpp file like this:
#include <opencv/cv.h>
When you've done this, delete the build folder. This is the folder with a very complicated name of the type build-untitled-Desktop_Qt_5_7_0_MSVC2015_32bit-Release. Then, in the Build menu, press "Run qmake". When you've done all this, it should compile fine.
Somehow it did not work for when I had several INCLUDEPATH +=.
When I combined the stuff into a single on it suddenly worked.

Linking dll to c++ in qt for reading .mat files

I am using Qt Creator 2.4.1(Based on Qt 4.7.4) ..
Now I want to open the .mat file which I got from Matlab.
I am using the basic function matOpen
Initially, I knew the headers required. They are mat.h, matrix.h and tmw.h.
Now, before running the code, I need to link the libraries. The required libraries as far as I know are libmat.dll and libmw.dll . I added them to the .pro file as follows.
QT += core
QT -= gui
TARGET = mat_open_test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += \
mat.h \
matrix.h \
tmwtypes.h
LIBS += -L"C:\Users\skanduri\Documents\C ++\mat_open_test-build-desktop- Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\debug\libmat.dll"
LIBS += -L"C:\Users\skanduri\Documents\C ++\mat_open_test-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\debug\libmx.dll"
Still I get error as : undefined reference to matOpen.. m sure the problem is with the linking.. But I dunno how to solve it. and the compiler Qt is using is MinGw .
You are specifying library path only, not the libraries itself. Try
LIBS += -L"C:\Users\skanduri\Documents\C ++\mat_open_test-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\debug"
LIBS += -llibmx
LIBS += -llibmat

Run time error for a missing DLL which exists

I am writing a tool with Qt Creator which builds but immediately crashes with the message:
"The program can't start because pthreadVC2.dll is missing from your
computer. Try reinstalling the program to fix this problem".
Of course, the "pthreadVC2.dll" library is not missing (and is not corrupted, since it works with other projects), and it is located in the path specified in the Qt pro file:
# DeltaPlots.pro
TARGET = DeltaPlots
QT += core gui
CONFIG += console
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TEMPLATE = app
win32 {
INCLUDEPATH += S:\\3rdparty\\DFS.Infrastructure.ThreadingW \
S:\\3rdparty\\DFS.Infrastructure.File \
"C:\\path\\to\\boost\\boost_1_51_0"
win32-g++:LIBS += -L"S:\\lib\\" -lMyLib
win32-g++:LIBS += -L"S:\\3rdparty\\DFS.Infrastructure.File\\" -lDFS.Infrastructure.FileSystem
win32-g++:LIBS += -L"S:\\3rdparty\\DFS.Infrastructure.ThreadingW\\" -lDFS.Infrastructure.Threading -lpthreadVC2
}
SOURCES += MainWindow.cpp \
entrypoint.cpp
HEADERS += MainWindow.h
FORMS += MainWindow.ui
OTHER_FILES += ProjectList.txt \
ImageList.txt
Platform:
Windows 7
MinGW
Qt 4.8.3
Qt Creator 2.6.0
[Edit: This answer refers to the original question]
Usually you would add
LIBS += -LS:/3rdparty/DFS.Infrastructure.ThreadingW \
-lpthreadVC2
This adds your library's folder as a library search path (Note the capital -L) and pthreadVC2.lib as a library to link against (lower case -l).
You do not need to add the .dll to the LIBS path, as the .dll is loaded at runtime.
But: This approach only works when the .lib is in the same folder as the .dll. I'm a bit surprised you have yours in different locations.
Maybe adding
LIBS += -LS:/3rdparty
would work, but I'm not sure about that.
In any event, you need to deploy the .dll with your .exe for releases.

qt how to link win32 libraries

I am using QT Creator 2.4.1. I want to make a network utility. I am using windows function:
GetAdaptersInfo().
This requires iphlpapi.lib to be linked.
To link the libraries I tried to add following argument in Projects->Build-Setup->Additional Arguments :
LIBS += -liphlpapi
or
win32:LIBS += -liphlpapi
or
LIBS += iphlpapi.lib
Without any success. I tried to give full path too. Can anyone tell me whats the correct way of linking?
This works:
Add win32:LIBS += -lIphlpapi in your .pro file;
clean (optional?);
run qmake;
build.

Resources