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.
Related
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.
I'm trying to create a custom QGIS desktop application with Qt 5.9.1
I have downloaded OSGeo4W (x64) geospatial software set and the Github C++ QGIS samples but I'm facing an error at compile time:
Cannot open include file: 'qgsmapcanvas.h': No such file or directory
Actually none of the above headers files are under OSGeo4W folder:
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgssinglesymbolrenderer.h>
#include <qgsmaplayerregistry.h>
#include <qgsrasterlayer.h>
#include <qgsmapcanvas.h>
But..shouldn't they be in dll libraries (eg qgis_core, qgis_gui ... ) ?
Anyway, this is my *.pro file:
TEMPLATE = app
TARGET = qgis_example4
QT = sql network svg gui core xml
LANGUAGE= C++
INCLUDEPATH += "C:\\OSGeo4W64\\include"
LIBS += -L"C:\\OSGeo4W64\\apps\\qgis\\bin" -lqgis_core -lqgis_gui -lgdal_i -lgeos_c
DEFINES+=CORE_EXPORT=__declspec(dllexport)
DEFINES+=GUI_EXPORT=__declspec(dllexport)
CONFIG += qt gui exceptions stl warn_on debug thread
RESOURCES += resources.qrc
FORMS += mainwindowbase.ui
HEADERS = mainwindow.h
SOURCES = main.cpp \
mainwindow.cpp
PS: there is a similar question on stackiverflow but I repeated it because the answers given did not give the solution
Solved: the needed headers file could be found within the "qgis-dev" branch of the project.
OSGeo4W : For users that cannot use OSGeo4W for some reason or just prefer it there is also a weekly snapshot of qgis-dev from OSGeo4W as standalone installer
After the installation you can find header in:
C:/Program Files/QGIS 2.99/apps/qgis-dev/include
So this is my .pro file
TEMPLATE = app
TARGET = qgis_example4
QT = sql network svg gui core xml
LANGUAGE= C++
INCLUDEPATH += "C:/Program Files/QGIS 2.99/include"
INCLUDEPATH += "C:/Program Files/QGIS 2.99/apps/qgis-dev/include"
LIBS += -L"C:/Program Files/QGIS 2.99/apps/qgis-dev/bin" -lqgis_core -lqgis_gui
DEFINES+=CORE_EXPORT=__declspec(dllexport)
DEFINES+=GUI_EXPORT=__declspec(dllexport)
CONFIG += qt gui exceptions stl warn_on debug thread
RESOURCES += resources.qrc
FORMS += mainwindowbase.ui
HEADERS = mainwindow.h
SOURCES = main.cpp \
mainwindow.cpp
Although the files are now present, I still not able to run QGIS samples. Maybe I'll move to other sample or I'll do my custom basic application.
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.
I have added qcustomplot.h/.c files to my Qt project. They are located in: "QT_PROJECT_DIR/QCustomPlot/".
Every time I use the designer in Qt Creator and build I get this error in ui_mainwindow.h:
error: ../../qcustomplot.h: No such file or directory
#include "../../qcustomplot.h"
This is of course true as it is located in: "QT_PROJECT_DIR/QCustomPlot/"
How do I change how Qt Designer auto generate this path?
If it helps, here is my .pro:
#-------------------------------------------------
#
# Project created by QtCreator 2014-01-12T00:44:44
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets widgets opengl printsupport
TARGET = Test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
QCustomPlot/qcustomplot.cpp
HEADERS += mainwindow.h \
QCustomPlot/qcustomplot.h
FORMS += mainwindow.ui
CONFIG += mobility
MOBILITY =
Your include seems to be wrong:
#include "../../qcustomplot.h"
Since you do not add the QCustomPlot folder to your INCLUDEPATH, you would need to include the file as follows:
#include "QCustomPlot/qcustomplot.h"
Alternatively, you could change the INCLUDEPATH as follows:
INCLUDEPATH += $$QT_PROJECT_DIR/QCustomPlot/
and then you could include the header without the ugly ../../ relative reference as follows:
#include "qcustomplot.h"
It is a rule of thumb to avoid such relative include paths in the source and headers files themselves because the structure can change at any time, or may be different on a different machine, et al. It is better resolved by the buildsystem and include paths.
I met the same problem and have solved it.
It was caused by the path of qcustomplot object in xxx.ui.
Step 1: Open the ***.ui file which contains the qcustomplot item.
Step 2: Right click the qcustomplot item
Step 3: Double click the path of QCustomPlot, then change it to QCustomPlot/qcustomplot.cpp from ../../customplot.
Finally, the header of ui_***.h can auto include QCustomPlot/qcustomplot.cpp, instead of ../../qcustomplot.h.
My native language isn't English. If you find grammar mistake in my reply, please inform me. Let me correct it.
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.