How do you link libraries in Qt? - c++

I have a project that I've written in VS2017 that has a lot of static libraries and I've got to the point where I want to start refining the gui. To make sure I can use Qt I made a test subdir program using the tips in https://www.toptal.com/qt/vital-guide-qmake, the https://wiki.qt.io/SUBDIRS_-_handling_dependencies example, the https://github.com/rainbyte/example-qmake-subdirs and https://github.com/219-design/qt-qml-project-template-with-ci/tree/6af6488d74e1dc97a8cef545080074055a696e9a projects as well as several of the similar questions here on SO.
This consists of a subdir project called "project" with 2 sub-projects "app" and "library". These are both simple QtApplications created using the New Subproject wizard, with all the files except for main.cpp removed from "app", everything left in except for main.cpp in "library", the point being to make sure mainwindow.h in "library" is referenced from main.cpp in "app". Despite trying every example I can find I'm still getting the "'mainwindow.h' file not found" error.
According to all the examples I could find you should only need to add a few lines (#) to the wizard-produced .pro files and add an additional .pri file to the "library" project;
Directory structure;
project/
project.pro
app/
app.pro
main.ccp
library/
library.pro
mainwindow.h
mainwindow.ccp
mainwindow.ui
library.pri
project.pro;
TEMPLATE = subdirs
TARGET = project ##
SUBDIRS += \
app \
library
app.depends = library ##
app.pro;
TEMPLATE = app ##
TARGET = app ##
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
SOURCES += \
main.cpp
include(../library/library.pri) ##
library.pro;
TEMPLATE = lib ##
TARGET = library ##
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
CONFIG += shared ##
CONFIG += lib_bundle ##
SOURCES += \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
DISTFILES += \
library.pri ##
and the added library.pri file;
LIBTARGET = library
BASEDIR = $${PWD}
INCLUDEPATH *= $${BASEDIR}/include
LIBS += -L$${DESTDIR} -llibrary
I know this has been asked many times already but I can find no definitive solution, or rather no-one seems to have bothered to post their working solution. I'd really appreciate any help with this, I'd love to be able to use Qt and escape the purgatory of win32 ui creation.

You have this:
INCLUDEPATH *= $${BASEDIR}/include
But you don't have directory named include anywhere, it seems. So probably remove the /include part from above.

Related

Importing libssh into Qt

I'm currently trying to import libssh into a Qt project I am working on. It appears that I have the library linked correctly as the project builds, but once I run the project it just crashes before anything really starts up. Which makes debugging difficult. If I comment out the line:
my_ssh_session = ssh_new();
Then everything runs as it should and no crash occurs. My .pro file looks like
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT += sql
QT += script
QT += scripttools
QT += uitools
LIBS += -llibssh
INCLUDEPATH += $$quote(C:\libssh\include)
TARGET = white_wrapper
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
datafeeder.cpp \
dbfeeder.cpp \
xmlhelper.cpp \
hiddevice.cpp
HEADERS += mainwindow.h \
datasource.h \
datafeeder.h \
dbfeeder.h \
xmlhelper.h \
hiddevice.h
FORMS += mainwindow.ui
After looking online it looks like the problem could be dlls but after running a dll dependency application it looks like I have all the dlls needed so I guess I'm at a lose and would love some input thanks.
I was getting the same error message with libssh-0.7.1, Qt 5.2.1 and MinGW.
After some research it turned up that libssh
needs another two dynamic libraries which depends on: libeay32.dll and zlib1.dll. They are not included in libssh's distribution archive, therefore you have to download them yourself and copy next to libssh.dll (or somewhere on $PATH).
In my .pro file I have the following lines added (I installed libssh using their installer to Program Files (x86) directory):
# Libssh:
LIBS += -L$$PWD/'../../../../../Program Files (x86)/libssh/lib/' -lssh.dll
INCLUDEPATH += $$PWD/'../../../../../Program Files (x86)/libssh/include'
DEPENDPATH += $$PWD/'../../../../../Program Files (x86)/libssh/include'
Now my Qt application works without problems.

Opencv sample in Qt crashes immediately with the error 'The program has unexpectedly finished'

I tried to create a simple Opencv application in Qt and upon running, the whole application is crashed. If I comment out the opencv related codes from the project, the project runs just fine.
What I did:
Downloaded the Qt 5.5.0 VS2013 64bit version
Downloaded the OpenCV 3.0
Downloaded the Cmake 3.3.2
There were already prebuilt binaries in when I extracted the Opencv package, But all tutorials on the net wanted me to recompile the source codes so did I. The package contains:
build
sources
created a new folder named mymade to hold the binaries next to the other two directories. So it now looks like this:
build
mymade
sources
Steps:
Fired up CMake, specified the source folder from the extracted files
and specified mymade as the output for binaries.
checked all options that had a opencv in their names, plus Qt! and configured it and subsequently generated the files.
This is the resulting contents:
So all is done and I now need to build the binaries. I opened OpenCV.sln and compiled the release and debug binaries. The dlls are placed inside bin directory, and the lib files are placed inside lib folder.
Now it's the time to configure the Qt projects .pro file, so I used the header files from build directory, and for libs I used the lib folder from mymade folder. This is the first configuration that I came up with, which compiles without any linker issues, but crashes just immediately.
The initial changes in project file :
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-06T14:04:20
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
INCLUDEPATH += L://Apps_Installation_Folder//opencv//build//include
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//*.lib
FORMS += mainwindow.ui
Doing this in project file as it was suggested by answers like this didn't do any good either:
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-06T14:04:20
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
INCLUDEPATH += L://Apps_Installation_Folder//opencv//build//include
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_core300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_highgui300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_imgcodecs300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_ml300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_objdetect300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_photo300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_shape300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_hal300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_flann300.lib
LIBS += L://Apps_Installation_Folder//opencv//mymade//lib//Release//opencv_features2d300.lib
FORMS += mainwindow.ui
This fails as well:
#-------------------------------------------------
#
# Project created by QtCreator 2015-10-06T14:04:20
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
INCLUDEPATH += L://Apps_Installation_Folder//opencv//build//include
LIBS +=-L"L://Apps_Installation_Folder//opencv//mymade//lib//Release"
LIBS += -lopencv_core300 -lopencv_highgui300 -lopencv_imgcodecs300 -lopencv_ml300 -lopencv_objdetect300 -lopencv_photo300 -lopencv_hal300 -lopencv_shape300 -lopencv_flann300 -lopencv_features2d300
FORMS += mainwindow.ui
So what is the problem here?
Update
As it was pointed out by #Miki, Qt couldn't find the opencv dlls (the configs above were OK but), And to solve this problem, one only needs to add the directory containing dlls to the PATH variable, there you are good to go and everything should be fine now.
From the comments to question it turns out that the program didn't find OpenCV dll.
You can:
Copy the required dll in the same folder of the .exe
Add the folder containing the dll to the PATH. You need to restart Qt Creator for changes to take effect.

Qt Including Files in Different Directories

So I've read a few posts and articles about how to go about doing this and one option was to do:
#include "../file/file.h"
But when I use this method, it still cannot find the file. The other way was to add the root of the project to the include path.
So here is an example directory:
main_dir
- dir2
-dir3
-header.h
-dir4
-dir 5
-source.cpp
So if I am trying to include header.h from source.cpp, how do I do this?
My .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2015-02-17T12:52:00
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = project1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
dir4/dir 5/source.cpp \
HEADERS += mainwindow.h \
dir2/dir3/header.h
FORMS += mainwindow.ui
There are many ways to solve the problem.
Use #include "../../dir2/dir3/header.h" in source.cpp.
If you can add main_dir to the list of include directories, (-I<main_dir_path>), then you can use #include "dir2/dir3/header.h" in source.cpp.
If you can add the toplevel directories under main_dir -- main_dir/dir2 and main_dir/dir4 -- to the list of include directories, then you an use #include "dir3/header.h" in source.cpp.

QT Project File - Field Meanings

I'm interested in know what some of the fields in a standard QT application project file means ? Here is a example
#-------------------------------------------------
#
# Project created by QtCreator 2013-04-02T11:06:19
#
#-------------------------------------------------
QT += core gui xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = AV_Command
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
case.cpp \
interview.cpp \
parsexml.cpp
HEADERS += mainwindow.h \
case.h \
interview.h \
parsexml.h
FORMS += mainwindow.ui
I understand what most of this is doing, except these 2 lines.
QT += core gui xml
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT += core gui xml means that the build process should expect to find and include QtCore, QtGUI and QtXML libraries for your project. If you don't use any XML (and especially don't use the XML headers) you can remove xml from that list.
On the flip side... if you wanted to add Qt's SQL stuff, you'd need to add sql to the list otherwise you would get linker errors complaining about missing definitions.
The greaterThan() compares QT_MAJOR_VERSION against 4. If true, then it adds widgets to the list of required QT libraries.
QT += core gui xml
Specifies what components of the Qt library you're using.
The second includes widgets depending on the version of Qt

make C++ Library with qt

i build a C++ library with qt creator.
qt builds for me in debug folder 3 files:
'1. libSerialize.a' and '2. Serialize.dll' and 'serialize.o' .
now i want to add this library to another qt project.
how can i do that?.
how can i include that library? .
where should i copy those? .
which one is necessary? .
this is my .pro file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = get_send_array
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp \
student.cpp
HEADERS += widget.h \
student.h
so thanks, please help me.
Use:
win32:LIBS += /path/to/the/lib/Serialize.dll
unix:LIBS += -L/path/to/the/lib/ -lSerialize
Serialize.dll is a windows dynamic load library, and libSerialize.a is an ar archive (you may reffer to it as a static library) and usually used in *nix systems.