qt how to link win32 libraries - qt

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.

Related

undefined reference qt opencv

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

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

Problem with linking

I made a custom library and i compiled it into a dll (qustom.dll).
Now i want to compile another project using this dll.
What i did is to import the header files in the project and add this line in my .pro file
LIBS += -lqustom
but i get "error: cannot find -lqustom"
also i tried
LIBS += qustom.dll
and
LIBS += -libqustom.a
qustom.dll is in the project directory
Also i tried
OTHER_FILES += \
qextserialport1.dll
but didn't work either
Am i missing something here?
Missing a path perhaps? -L/path/to/dlls -ldllname
Qt plays nicely with CMake. I use that and I never get dependency/path headaches like this any more.

How to use CTelephony API with Qt Symbian

I want to use CTelephony API in Qt Symbian project but after including etel3rdparty.h and etel3rdparty.lib as library and header in .pro file like this:
LIBS += C:/NokiaQtSDK/Symbian/SDK/epoc32/release/armv5/lib/etel3rdparty.lib
INCLUDEPATH += C:/NokiaQtSDK/Symbian/SDK/epoc32/include
I am getting a lot of compilation errors.
Please share if someone faced and find the solution of this problem.
You are not supposed to include each library with the complete path in the .pro file. You need only the name of the lib, without the extension, prefixed with -l, i.e. in your case
LIBS += -letel3rdparty
Also, you would normally not need to add epoc32\include to the include path, as it's added by default.

Resources