QtCreator: How to compile external source files - c++

I have my C++ project files and want to create an additional graphical user interface for these sourcefiles. I am using windows, MVSC2012 and Qt 5.1.1 with Qt Creator 2.8.1.
So here is what i have:
My QtCreator project folder, including the following auto-generated files
c:/creatorProject/creatorProject/main.cpp
c:/creatorProject/creatorProject/mainwindow.cpp
c:/creatorProject/creatorProject/mainwindow.h
c:/creatorProject/creatorProject/mainwindow.ui
c:/creatorProject/creatorProject/creatorProject.pro
c:/creatorProject/creatorProject/creatorProject.pro.user
Furthermore I the source files with the "logic" in a separated folder, e.g.
c:/programLogic/myFunctions.h
c:/programLogic/myFunctions.cpp
So I simply want to add these files to my QtCreator project so that I can e.g. include "myFunctions.h" and work with it.
My attempt: I used Qt Creator and added myFunctions.h respectively myFunctions.cpp by using "creatorProject >> right click >> add existing file..". After doing that my creatorProject.pro looks like this:
[...]
SOURCES += main.cpp\
mainwindow.cpp \
../../programLogic/myFunctions.cpp
HEADERS += mainwindow.h \
../../programLogic/myFunctions.h
Looks totally fine for me. Qt Creator even shows these files within the project explorer! However I have trouble using myFunctions.h within mainwindow.cpp.
#include "myFunctions.h" // Include can not be found
#include "../../programLogic/myFunctions.h" // Include is found but I get linker errors since myFunctions.cpp is not compiled?!
What is wrong within my setup?

You need to add the INCLUDEPATH also in your .pro file.
Something like:
INCLUDEPATH += "C:/programLogic"

Related

Qt compiler cannot find the header file

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/

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.

QtTest Unit Testing, how to add header files located in another project?

Maybe I'm missing something, but it seems really stupid to me that the only tutorial provided for QtTest framework has you testing the QString class.
The typical use case for unit testing is....testing classes you wrote yourself, but there is no mention on how to reference your classes in a different project for testing in the tutorial and google has failed me as well(and I really doubt copy pasting classes is a good way to do it).
I even thumbed through 3 different Qt books with no mention of QtTest.
You can add include paths to other project directories in your .pro file like so:
INCLUDEPATH += <directory>
Then it should be able to find the headers that you are including.
Edit: Based on comment
That's another story altogether. Undefined reference usually means you are missing a dependency. This can usually be resolved with one of two things.
The simplest is to include the missing source file:
INCLUDEPATH += ../myotherproject/
SOURCES = main.cpp ../myotherproject/missingsource.cpp
Perhaps the better solution is to expose reusable code by compiling it as a library and linking to it. E.g. a .DLL or .LIB on Windows and .SO or .A on Linux.
INCLUDEPATH += ../myotherproject/
win32:LIBS += ../myotherproject/linkme.lib
Can you show us the specific errors you are getting?
I suggest that you put all sources and headers which both your main application project and your unit test project need into one .pri (.pro include) file. Put this file in the main project. Then include this file in both projects.
Note that whenever adding a new class to the main project, QtCreator automatically appends the SOURCES += and HEADERS += lines to the .pro file, but you want them to be in the .pri file, so you need to move them afterwards manually. I think that there is no solution to tell QtCreator where to put them.
Main project:
myproject.pro
myproject.pri
main.cpp
someclass.h
someclass.cpp
myproject.pro:
QT += ...
TARGET = ...
...
SOURCES += main.cpp # "private" to this project
include(myproject.pri) # needed in unit test
myproject.pri:
SOURCES += someclass.cpp
HEADERS += someclass.h
Unit test project:
unittest.pro
main.cpp
test.h
test.cpp
unittest.pro:
QT += ...
TARGET = ...
...
SOURCES += main.cpp test.cpp
HEADERS += test.h
# include the classes from the main project:
INCLUDEPATH += ../myproject/
include(../myproject/myproject.pri)

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.