link opencv 2.4.6 with Qt library - c++

I am working on windows 7, and installed and compiled OPENCV 2.4.6, it worked perfectly well with microsoft studio 2012, so I know how to find the path of OPENCV include and lib, and configure the path of bin in the system variables
But when it comes to QT, it totally didn't work at all. I know on the stackoverflow there are many questions about how to link opencv with QT in windows, But I tried all the answers, but it never worked out for me.
Here is my configuration which works for visual studio 2012.
system variable path:
D:\Program Files\opencv\build\x64\vc11\bin;
include path:
D:\Program Files\opencv\build\include\;
D:\Program Files\opencv\build\include\opencv;
D:\Program Files\opencv\build\include\opencv2;
lib path:
D:\Program Files\opencv\build\x64\vc11\lib;
I have downloaded and installed QT 5.1.1, and started a project. In the pro, I edited like this,
INCLUDEPATH += D:\Program Files\opencv\build\include \
I assume this should be working, but in the main(), when I added the header
#include "opencv/cv.h"
#include <opencv/highgui.h>
QT compiler will always tell me 'no such file or directory'
This kind of stupid problem has stuck me in for days, I don't know where it goes wrong, could some people help me.
update:
Thanks for everybody, Gibby's answer has helped me to solve the first issue of 'headers can't be found', but now then running the program, I got issues like 'release\test.exe:-1: error:LNK1120: 6 unresolved externals'
so it is definitely due to the linking of libraries , I have tried
LIBS += -LD:\Program Files\opencv\build\x64\vc11\lib \
-lopencv_core246 \
-lopencv_highgui246 \
-lopencv_imgproc246 \
-lopencv_features2d246 \
-lopencv_calib3d246 \
or
LIBS += -LD:\Program Files\opencv\build\x64\vc11\lib \
-lopencv_core246.lib \
-lopencv_highgui246.lib \
-lopencv_imgproc246.lib \
-lopencv_features2d246.lib \
-lopencv_calib3d246.lib \
or from How to link opencv in QtCreator and use Qt library
LIBS += -LD:\Program Files\opencv\build\x64\vc11\lib \
libopencv_core246 \
libopencv_highgui246 \
libopencv_imgproc246 \
libopencv_features2d246 \
libopencv_calib3d246 \
but none of these work

You need to build OpenCV with Qt. The steps are,
Install Qt on your computer (I use Qt4 for all versions of OpenCV since my applications are based on it but you can use Qt5 as well).
Make a note of the install path (for me, it is D:/Qt/4.8.5/ and put it in your Path environment variable.
Open CMake and start the build process for OpenCV. Make sure you select the option WITH_QT under CMAKE.
My CMAKE_INSTALL_PREFIX ss D:/Program Files/opencv and it is there on my Path environment variable`as well; you can change yours accordingly.
Build with other options are required. Generate the files and then build the binaries and install OpenCV in the above mentioned path.
Your OpenCV installation now supports Qt and you can use it inside VS and Qt Creator. For further reference, please head to this link.
HTH

I had the same problem and resolved it by setting the project to release(bottom left in the qt window) instead of debug and then copying the qt and opencv library into the bin folder. Once you have set your project to build a .exe file will be generated in the bin folder. running that application will tell you what dll is still missing, find the opencv dll's at opencv/build/x64/vc11/bin and the qt ones at Qt/Qt(version)/(version)/(compiler)/bin
~Gibby

Because of the space character.
qmake might consider "D:\Program" and "Files\opencv\build\include" as separated.
Just surround your path with double-quotes.

Related

How to install and add libpng to my Qt project on Windows

I need to use libpng for my coursework with Qt. But I don't know how to install it and use with Qt on Windows.
Try to install with MinGw
Firstly, I've downloaded libpng and zlib from official site. Then I've tried to install it with MinGw, ./configure CFLAGS='-O2' CXXFLAGS='-O2' --prefix=/mingw this command executes perfectly, but then I got a problem. After typing make and make install I've got following issue. I don't know how to fix it and what it actually mean, because I've never faced errors from makefiles.
Try to install via application
I've found an application for installing libpng on windows: http://gnuwin32.sourceforge.net/packages/libpng.htm. And it worked, I've installed a library, got my lib files and headers. Then I went to my Qt project, and added a library, by pointing path to lib file and path to headers. Now I'm including png.h in my class and things seems to go well, until I try to use some functions. Qt writes something like this No mathcing function for call to png_sig_cmp. But more interestingly, that it actually don't show errors when I use variable types from library, for example png_byte.
//messege from MinGw after typing make and make install
rm -f pnglibconf.c pnglibconf.tf[45]
gawk -f ./scripts/options.awk out=pnglibconf.tf4 version=search\
./pngconf.h ./scripts/pnglibconf.dfa\
./pngusr.dfa 1>&2
gawk -f ./scripts/options.awk out=pnglibconf.tf5 pnglibconf.tf4 1>&2
options.awk: bad line (10): com
make: *** [pnglibconf.c] Error 1
I expect to install library on my disk and be able to include it in other projects in other IDE fast.
For your build problem Google search yields: https://github.com/aseprite/aseprite/issues/1054#issuecomment-335846694 .
Suppose you built not libpng, but libcurl as I did a while ago with mingw compiler,
then to use libcurl within your Qt project file do:
win32 {
## Windows common build here
win32-g++:contains(QMAKE_HOST.arch, x86_64):{
message("x86_64 build")
INCLUDEPATH += C:\QT64\Lib64\curl-7.40.0-devel-mingw64\include
LIBS += C:\Qt64\Lib64\curl-7.40.0-devel-mingw64\lib64\libcurldll.a
# Win64 specific build here
} else {
message("x86 build")
INCLUDEPATH += C:\QT\Lib\curl-7.40.0-devel-mingw32\include
LIBS += C:\Qt\Lib\curl-7.40.0-devel-mingw32\lib\libcurldll.a
#Win32 specific build here
}
}

QT Creator: OpenCV dll lib can not be added ("File format not recognized")

I'm trying to get openCV 3.1.0 to work in QT Creator 4.4.1 using this tutorial from their official site.
Right now, I'm setting up my project file. The whole file looks identical to the on in the tutorial apart from the last couple of lines, as my openCV build is in another place and it's a different version This is what I got:
INCLUDEPATH += C:\opencv\opencv-3.1.0\build\include
LIBS += -LC:\opencv\opencv-3.1.0\build\x64\vc14\bin \
libopencv_world310d \
As far as I understand, the "world" module has been introduced to openCV to unify all the different modules into one.
However, when I try to run this, QT produces the following error:
C:\opencv\opencv-3.1.0\build\x64\vc14\bin\opencv_world310d.dll:-1: error: file not recognized: File format not recognized
What am I doing wrong? In the tutorial there's an alternative way to link the libraries which even explicitly states them as .dll files. Why is this a problem?
You need to link to the .lib contained in the lib folder:
LIBS += -LC:\opencv\opencv-3.1.0\build\x64\vc14\lib \
and put the .dll in the PATH, or the same folder of the .exe

Linking OpenBLAS with Qt Creator project

I am using Qt Creator for development. I want to use Armadillo linear algebra library. Therefore I used following code in my pro file:
INCLUDEPATH += C:\armadillo-7.950.1\include
LIBS += \
-LC:\armadillo-7.950.1\examples\lib_win64 \
-llapack_win64_MT \
-lblas_win64_MT
It was all good and the project compiled and linked properly. Next task was to replace LAPACK and BLAS with OpenBLAS. So the pro file was changed as following:
INCLUDEPATH += C:\armadillo-7.950.1\include \
C:\OpenBLAS-v0.2.19-Win64-int32\include
LIBS += \
-LC:\OpenBLAS-v0.2.19-Win64-int32\lib \
-llibopenblas
Now I receive the error: LNK1104: cannot open file 'libopenblas.lib'. I have downloaded windows binary of OpenBLAS. The installed folder structure is as given in the image: installed OpenBLAS folder
Please let me know what is going wrong?
Thanks.
There are two reasons for this problem:
Weird naming of .lib file as libopenblas.dll.a even though package is called windows binary.
Incomplete binary package---- It doesn't contain all required dlls
Solution:
Choose a binary----win 64 or win32 and download it.
rename libopenblas.dll.a as libopenblas.lib as Yuriy has suggested
download from https://sourceforge.net/projects/openblas/files/v0.2.14/ required missing DLLs are bundled again under weirdly named zip files "mingw64_dll" or "mingw32_dll" depending upon the binary type which you have downloaded in step 1. These zip file contain "libgcc_s_seh-1.dll", "libgfortran-3.dll" and "libquadmath-0.dll"
Add include and lib as I have done to your project and build with correct target depending upon your downloaded binary i.e win64 or X86
Copy libopenblas.dll from bin folder of OpenBLAS binary and "libgcc_s_seh-1.dll", "libgfortran-3.dll", "libquadmath-0.dll" from "mingw64_dll" or "mingw32_dll" depending upon the binary type to build debug or release directory where the exe reside.
Run your program and you are good to go.
If you want to escape copying again and again set a required system environment variable for the path containing these DLLs
I'm not familiar with OpenBLAS, but your configuration looks correct.
On Windows, or at least with MSVC, you need a .lib file to link to, in addition to the DLL file for runtime. Instead it looks like OpenBLAS installs a mysterious .dll.a file. Sounds like it should be the same thing:
https://github.com/xianyi/OpenBLAS/wiki/How-to-use-OpenBLAS-in-Microsoft-Visual-Studio
Possibly renaming it would do the trick, but you might need to rebuild from source to get the file.

z.lib problems while porting qt creator projet to windows

I am trying to port a Qt5.9 project from Mac to Windows 10.
I was able to compile the project easily in a ubuntu installation.
While trying to build it for windows, i had problems with finding zlib include headers with
#include<zlib.h>
That i corrected after following answers here on Stack to
#include<QtZlib/zlib.h>
Now i have problems in LINK phase, it can not open the file z.lib
Problem is i downloaded zlib packages, builds, source code and could not find a z.lib. Only different named libs. Searching in google i could only find people with the same problem, z.lib is not one of the libs included in zlib installation.
This is my project file:
TEMPLATE = app
QT += qml quick widgets websockets
CONFIG += c++11
SOURCES += \
main.cpp \
api.cpp \
app.cpp
HEADERS += \
api.hpp \
app.hpp
RESOURCES += qml.qrc
LIBS += -lz
I tried putting all possible dll and lib files in the project folder. None of them is named z.lib though.
The symbols for zlib are already part of the qt libraries. As long as you do not try to link the zlib explicitly it should work. At least it does work for me.
add to your project file:
!win32 {
LIBS += -lz
}
I managed to solve my problem updating my Qt installation to use MinGw 5.3 32bit. I was using VisualStudio 2015 as the compiler before.
Only changing the compiler to MinGw (g++) 5.3 made everything work with the same pro file i posted in the original question. Thanks everybody who tried to help!

How to Link Imagemagick library to Qt(windows)

I have developed an application with mac and for one month now, Im trying to link Qt with ImageMagick on windows.
I just cant find the files and the version of Imagemagick library that need to be linked to Qt in order to make my application work on windows.
I have tried most of the libraries from here
Thats the only thing I added to the .pro file while I was developing on a mac
INCLUDEPATH += . /opt/local/include/ImageMagick
LIBS += -L/opt/local/lib -lMagick++
When I add this to my .pro
INCLUDEPATH += C:/im6/include/ImageMagick
LIBS += C:/im6/lib/libMagickWand.a
LIBS += C:/im6/lib/libMagick++.a
LIBS += C:/im6/lib/libMagickCore.a
C:/im6/lib/libMagick++.a
I get 10.000 + simmilar errors to that:
(Image.o):C:\msys\1.0\home\cristy\ImageMagick-6.6.6-0/Magick++/lib/Image.cpp:4157: undefined reference to `__gxx_personality_sj0'
C:/im6/lib/libMagick++.a(Image.o):C:\msys\1.0\home\cristy\ImageMagick-6.6.6-0/Magick++/lib/Image.cpp:4157: undefined reference to `_Unwind_SjLj_Register'
C:/im6/lib/libMagick++.a(Image.o):C:\msys\1.0\home\cristy\ImageMagick-6.6.6-0/Magick++/lib/Image.cpp:4178: undefined reference to `_Unwind_SjLj_Unregister'
For more errors check this
Has anyone tried to build and deploy an ImageMagick application using qt on windows ?
Which files do I need to link while building and which files while deploying ?
The error message tells me that you
use mingw (with qtcreator). Your
library might not work with this
compiler. You need a mingw version
of the library, probably named
libmagick.a.
LIBS += -L$$quote(c:/Program Files/ImageMagick-6.6.5-Q16/lib) -lmagick
(see the documentation about qmake Project Files
I have found that the minGW tool set is very much treated as a "stepchild" in the Windows world. I know it doesn't help your particular situation, but we had numerous problems with third-party libs while trying to use Qt with minGW. There were always extra steps, or non-existent build instructions that we had to figure out or tweak.
Once we switched to the MSVC version, all of those problems went away. Note that this doesn't mean you have to use Visual Studio (you can still use Qt creator), it just means that the Visual Studio compiler is being used. You can get the compiler for free using the Platform SDK. If this is an option, you might save yourself a lot of pain by switching now. It's unfortunate, but that is just the current status quo, at least as we discovered.
Please go get ImageMagick source package.
I can’t give direct instructions how to work with MinGW of Qt Creator, but basically following. I prefer to use qtvars.bat that comes with binary distributions of Qt to set environment.
Open Windows cmd
Set PATH to have /path/to/mingw/bin and /path/to/qt/bin before other paths
Go to directory where you have extracted ImageMagick source
read README, INSTALL and such to compile the ImageMagick (most likely just ./configure ; mingw32-make, but never be sure)
Edit your .pro file with something like LIBS += -L$$quote(/path/to/imagemagick/lib/dir) -lmagick . The lib may be in bin dir or in lib dir. See hmuelner’s answer for more information.
At this point, the configuration should be ready. Compilation at Qt Creator should work, but you can as well use this cmd windows to compile your program: go to program directory, run “qmake” and “mingw32-make”.
You cannot link against a ImageMagick++ library built with MSVC, as mingw and MSVC DLLs are incompatible for C++ libraries. You can't link a MSVC-built C++ library into a mingw project, nor vice versa. C-only libraries work fine. Also, according to this fortum thread, using builds against old mingw-versions with current mingw versions doesn't work as the exception handling changed (as you get more errors than that, I wouldn't expect that to be the only problem though). If there is no binary package for your mingw version (and I don't see any on the imagemagick website), your only option is to use a source package, as Smar suggests.
Another option of course is to install Visual Studio, download Qt for MSVC and build your project with MSVC.