Qt compiler cannot find the header file - c++

I know it's a quite common issue but I haven't found a comprehensive answer on the following question.
I have Qt 5.4.1 MSVC2013 build running on Windows 8.1.
Here is a look on my project files:
And here is what my .pro file looks like:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Vfp
TEMPLATE = app
QMAKE_CXXFLAGS = - std=c++11
SOURCES += main.cpp\
Views/mainview.cpp
HEADERS += Views/mainview.h
FORMS += Views/mainview.ui
The problem is, unless I add INCLUDEPATH += Views/ to the .pro file, I cannot include mainview.h in main.cpp file.
Why? Shouldn't HEADERS += Views/mainview.h be enough?

If you're including the header file this way:
#include "mainview.h"
Then yes, you need to add that include path since the compiler (not the IDE) doesn't know where mainview.h is.
Otherwise, you need to specify the relative path to the file, like:
#include "Views/mainview.h"

Documentation doesn't say that HEADERS is for specifying include paths to the compiler. HEADERS is used for generating dependency information and checking whether moc is necessary.

If you add below command to .pro file you will able to compile it.
INCLUDEPATH += ...path/Views/

Related

Qt can't find my shared library - no really it cant

I'm new to Qt and I'm trying to implement a simple shared library for use in another application. I've read the wiki and other docs, but even after carefully following the steps, the compiler will always throw an error saying it can't find my header files.
I'm using Qt MinGW on Windows 10. Here's what I did:
Created and compiled a shared library called libgx
In another project (hello), added the generated liblibgx.a file, using the "Add Library" wizard.
Pulled my hair over this error I get when I try to include the library's header into hello's main.cpp
The wiki is not helpful here at all. It tells me, "MinGW compiled linking libraries are in .a, but you will need to add it manually (as of Qt Creator 2.7). You could also try simply linking the .dll directly cause it would probably work." I'm like WHAT?
hello's .pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = hello
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp\
C:/Users/sagar.JARVISAIO/Documents/libgx/libgx.cpp
HEADERS += mainwindow.h\
C:/Users/sagar.JARVISAIO/Documents/libgx/libgx.h
FORMS += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/release/ -llibgx
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/debug/ -llibgx
else:unix: LIBS += -L$$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/ -llibgx
INCLUDEPATH += $$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/release
DEPENDPATH += $$PWD/../build-libgx-Desktop_Qt_5_5_0_MinGW_32bit-Release/release
I manually added the headers and the library files in the .pro file using the procedure here. Never going to bother with that "wizard" again. It was unnecessarily adding a $$PWD variable in all my paths which I think was causing my problem.

Qt won't recognize headers from sub directory /Include

Environment
I am on Windows 7 64bit
Qt Creator 3.4.2 (opensource)
Based on Qt 5.5.0 (MSVC 2013, 32 bit)
Goal
I am using a 3rd party library. I need to #include the library files like that since that is how the library files include their dependencies.
What I have tried
in myFunction.h (in same directory as .pro file)
#include <lib_header.h>
...
in .pro file
LIBS += -L"$$PWD/Debug/" -llib_name
SUBDIRS += "$$PWD/Include"
DEPENDPATH += "$$PWD/Include"
INCLUDEPATH += "$$PWD/Include"
DEPENDPATH += "C:/Users/Steves Laptop/UX3D/Include"
INCLUDEPATH += "C:/Users/Steves Laptop/UX3D/Include"
VPATH += "$$PWD/Include"
VPATH += "C:/Users/Steves Laptop/UX3D/Include"
...
lib_header.h is in /Include
Results
I get tool tip when hovering over the #include line of the exact location of the file. On compile it says it can't be found. I expect this is a newbie mistake but have spent 3 hrs wasted so far. If in my files I do it like this #include <Include/lib_header.h> it works. I need a way to reference them directly though in order for the rest of the library to work.
Even if someone could provide a link or reference source file that documents Qt's implementation of the #include <> tag that would be sufficient.

QT Creator fails to parse a very easy project

I created a very simple project in QT creator, *.pro file is just following:
HEADERS += inc\1.h
SOURCES += src\1.cpp
Here is the source code:
// 1.h
const int C = 1;
// 1.cpp
#include "1.h"
int main() {
return C;
}
QT Creator successfully opens this "project", but cannot parse it. F2 does not work for C constant, 1.h header cannot be found.
Please look at the screenshot which describes the problem:
The most strange part is that exactly the same thing seems to work on my other machine with similar QT SDK 5.0 installation! Could you please advise where am I wrong?
HEADERS is supposed to list the header files of your own project, just like SOURCES lists the source files.
If you want to include external header files, you should add their folders to INCLUDEPATH instead:
INCLUDEPATH += inc
The following .pro file works perfectly in Qt Creator 2.5.0, Qt 4.6.1:
QT += core
QT -= gui
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += \
src/1.cpp
INCLUDEPATH += inc
Try that exact .pro file, give it 3 seconds to parse and tell me if it worked.

Using SOCI with Qt = how to write a good *.pro file?

I would like to write a GUI app using Qt and SOCI. How to write a good *.pro file to compile a project with no errors? I wrote this:
QT += core gui
TARGET = example-project
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += /usr/local/include/soci\
/usr/include/postgresql/
LIBS += -lsoci_core -lsoci_postgresql -ldl -lpq
and it works but I don't know if it's correct.
The .pro file you wrote looks good, the INCLUDEPATH /usr/include/postgresql/ may not need a trailing slash, however, the way to tell if it will produce "no errors" is to try it. The INCLUDEPATH definition will let you use headers from those directories like this:
#include <header.h>
instead of:
#include "/usr/include/postgresql/header.h"
The LIBS+= section should only contain the libraries from SOCI that contain symbols that you reference in your code. If you are statically compiling your program it will bundle these libraries into your binary, increasing its size.
There are a lot of features you can easily add with the .pro file, and it's helpful to know how to write one, for example you can add an application icon for Mac OS programs by adding the line:
ICON = Icon.icns
Have a look at the Qt 4.7 .pro file reference.
You can always use an auto-generated .pro file by navigating to the directory that your source is in (in a terminal), and using the command:
qmake -project
In my experience, the auto generated .pro file is usually incomplete, but it gives you a standard of comparison and sometimes includes stuff that you would otherwise forget.
A final method of .pro file authoring is from the QtCreator IDE. It automatically adds and subtracts things from your pro file as you add/subtract them from your project, it's especially simple to add forms and resources in this environment.

Qt Creator and static libraries

I'm quite a newbie with C++ and maybe that's a very stupid question, but how do one include a header from a static linked library?
I've created a static library in Qt Creator with the following .pro file:
QT -= gui
TARGET = Foobar
TEMPLATE = lib
CONFIG += staticlib
SOURCES += thefoobar.cpp \
sub/subbar.cpp
HEADERS += thefoobar.h \
sub/subbar.h
compiled it and put the resulting libFoobar.a into the "extstaticlibs" folder of my target project.
In my target projects .pro file i've added the following lines:
LIBS += -L$$PWD/extstaticlibs/ -lFoobar
INCLUDEPATH += $$PWD/extstaticlibs
The target project compiles without problems. But when I try to include the header thefoobar.h in one of my code files:
#include "thefoobar.h"
it always results in an error:
error: thefoobar.h: No such file or directory
Any suggestions for the correct syntax would be very much appreciated.
Kristoffer
Check where you have placed your "thefoobar.h" header file . Place it in the "extstaticlibs/" folder .
If I follow your description correctly, you ONLY put the static library into your extstaticlibs directory.
You need to carry over your thefoobar.h file too. If you follow the common structure you could make:
extstaticlibs/include <- thefoobar.h goes here
extstaticlibs/lib <- libFoobar.a goes here
You then need to modify your project file like this:
LIBS += -L$$PWD/extstaticlibs/lib -lFoobar
INCLUDEPATH += $$PWD/extstaticlibs/include
Of course you can all throw it in one directory, if you want, but it may be helpful to sort things out in the beginning.