Qt Creator: include directories - qt

I'm trying to create a GUI and i'm having issues with some header files that the program needs and uses.
If i click on the function in the main.cpp it takes me to the header file. But for some reason when i compile the program I get "undefined reference to " the function in main.cpp.
Ive tried to add the path of the include folder where the header file in the .pro file but it didnt work.
It seems to me that Qt sees the function but somehow it doesnt compile.
Any suggestions? Thank you

This is my solution to the case of “undefined reference to” error,
In .pro file append or modify this line:
QT += core gui sql printsupport network websockets
Maybe that's the same thing for you.
There is a similar description in the Qt document
Header: #include <QWidget>
qmake: QT += widgets
You can find which header file corresponds to which module

for some reason the compiler did not see the library that the header file needed. it was specified in the makefile but it didnt work.
in the .pro file i added
LIBS += -lmylib
and that fixed the issue. Thank you for your help

Including a header so the compiler can find the declarations of classes/methods/functions is one thing.
Adding source code or libs so that the definition of the declared facilities can be found by the linker is another.
I get "undefined reference to " the function in main.cpp
This is a linker error. You either haven't added your own source files to the project, or any third-part libs you use. Open the context menu of your project in the left Creator pane and select "Add existing file..." for the first case or "Add library..." for the second.
And in the next step you should spend some time reading the Creator manual as well as some basics about C++ and compiling in general. All beginner questions like the above have already been answered multiple times, you just have to search for them.

Related

Unable to add include path in Qt Creator

I've downloaded a C++ project which uses Boost. It's rather complicated and plenty of files reference its parts, like:
#include <boost/graph/fruchterman_reingold.hpp>
I've put Boost directory in the project folder and added INCLUDEPATH += "C:/Programming/my-project" in the .pro file but for some reason Qt keeps telling me "No such file or directory" about every single file. Now note that if I change the paths to absolute the references start working. I've ran Qmake explicitly but still get the same problem. What can I do about it besides changing all paths to absolute?
I'm running Qt Creator 3.3.0, Qt 5.4.0, the compiler is MinGW 4.9.1.
So I've figured it out. Apparently the project had several projects inside but I wasn't aware that I had to change .pro files in every single one to tell the compiler to add a new path. After I did it the problem was solved. Thank you for your time, people.

How to know what to add to *.pro file in QT project when including new library?

While doing some newbie development in Qt I faced with challenge to understand what to add to my *.pro file.
For example, I'm adding #include <QDomDocument>. After save it starts saying that
error: QDomDocument: No such file or directory
. I go to Google, past this error and find on Stackoverflow what to add to my .pro file.
How this QT += xml is named and where to find it in documentation?
Try search something about Qt in Qt Creator help:
Press Help (left side)
Type qdomdocument in Search for:
Choose QDomDocument class.
Now you see this table:
Header: #include <QDomDocument>
qmake: QT += xml
Inherits: QDomNode.
It is answer, you can find it in documentation. And you'll know where you should search.
Of course you can find this in Internet, but for example:
This link has qmake line: http://qt-project.org/doc/qt-5/qdomdocument.html
This link hasn't qmake line: http://qt-project.org/doc/qt-4.8/qdomdocument.html
Help in Qt Creator always gives you actual info, but Google first of all give you qt-4.8 documentation.

Qt creator can not include opencv header files

I have downloaded qt-5.0.0 for windows.
http://releases.qt-project.org/qt5/5.0.1/qt-windows-opensource-5.0.1-msvc2010_32-x86-offline.exe
I have added INCLUDEPATH += C:\opencv\build\include in the .pro file.
Opencv 2.4.3 is already installed.
When I include header file in qtcreator :
#include <opencv2/opencv.hpp>
There is compilation error : can not find opencv2/opencv.hpp
Any ideas ??
I found the solution. In Qt Creator, goto Projects on the left pane ( ctrl+5), then Build Environment -> Use System Environment, click on Details. Edit LIB variable. Add here.
Things to check:
does C:\opencv\build\include\opencv2\opencv.hpp actually exist?
does the compile command (which you can check in the Qt Creator "compile output" window) show -IC:\opencv\build\include argument in the compile command?
qmake should be run automatically after modifying the .pro file, but re-run it manually just in case (for example from Build menu), as suggested by the first answer
this should not have any effect in issue like this, but just in case: if you are using "shadow build" (which is a good idea), make sure the source dir is clean of any generated files
The correct header files are:
<opencv2/core/core.hpp>
<opencv2/imgproc/imgproc.hpp>
<opencv2/highgui/highgui.hpp>
… and so on. The include of "opencv.h" is deprecated!
It also does not work anymore for QtCreator auto-completion.
The solution is to update your sources to use the correct header files.
After any change made to .pro file , Do right-click on project folder and click on run qmake .
Well, I just ran into this problem tonight. luckily enough, after taking a fair time, the solution was found. If your project is managed by qmake, and Qcreator is used, just go to the Build->Run qmake, then build and run your project. A tip, whenever you change your *.pro file, remember to rerun Build->Run qmake, because that will reconfigure your project.
If this helps you, please give me a thumb up :)

Undefined references - I'm including correct header

I'm trying to subclass from ProjectExplorer::ProjectExplorerPlugin but I'm getting error telling me about undefined references. Any ideas how to fix it?
class MyPluginPlugin : public ProjectExplorer::ProjectExplorerPlugin
{
Q_OBJECT
...
};
error: undefined reference to `imp__ZN15ProjectExplorer21ProjectExplorerPluginC2Ev'
The fact that you don't get a compilation error, but an undefined reference usually means that your project knows where the header files are, but it doesn't know where the library is which contains the already compiled source code.
I've never written a plugin for Qt Creator but I've taken a quick look at its source code structure and I see the following options:
Option A)
There is a projectexplorer.pro file in Qt Creator's source under src/plugins/projectexplorer. You could manually build that project in order to get a ProjectExplorer.lib (plus a .dll or a .a) and then reference this library.Example: Assuming the library would be created in the same directory as its .pro file (I have no idea if it is like that) and you created your plugin withing Qt Creator's source under src/plugins/myplugin, you would define your LIBS variable like this:
LIBS += -L../projectexplorer \
-lProjectExplorer
The first line adds "../projectexplorer" as an additional library directory and the second line adds "ProjectExplorer" as a library to search in any of the defined directories (it automatically adds the OS-specific file extensions like .lib on windows etc).
Obviously if your project or the library is located somewhere else, you need to change the first line accordingly.
Option B)
You could include the source and header files of the projectexplorer directory to your own .pro file using the HEADERS and SOURCES variables. I'm not sure if this wouldn't interfere with any other plugins (including projectexplorer itself) though.
Option C)
There probably is a way to include the projectexplorer.pro file so that you have a master project which first builds the project explorer library and then your own plugin. This would be the safest way to go as it ensures the Project Explorer library is built and up-to-date before your own project is linked against it.
However I have limited experience on this.
If anyone reading this can give a detailed explanation on this option, feel free to edit or provide your own answer.
If you are using Qt Creator built from source coded after April 2013 which includes Commit: #66a3553 - make library and plugin dependencies declarative, then you can simply specify dependencies for your plugin in its .pro file:
# myplugin.pro
QTC_PLUGIN_DEPENDS += \
coreplugin \
projectexplorer

How to add pre processing defs (macros) to qt creator?

In Eclipse there is an option to specify pre processing defines (#ifdef macros) to a project by using the Symbols option in Paths and Symbols. This helps in effective indexing of code which is cross platform. Is there any option to provide these in Qt creator?
It depends:-)
The following is assuming you are using qmake based projects:
First you can add DEFINES += SOME_DEFINE=value into your .pro file. That is picked up inside creator and when building on the command line and should also show up when creating a MSVC or XCode project from the .pro file.
Then you can add DEFINES += SOME_DEFINE=value to the qmake call that Qt Creator will issue when configuring the project. That happens in the Project Mode, Build Settings, QMake Steps.
Finally, you can put #define SOME_DEFINE value liens into a header file and include that. That works for all kinds of projects:-)
From the QT Documentation:
The defines are specified in the .config file. The .config file is a
regular C++ file, prepended to all your source files when they are
parsed. Only use the .config file to add lines as in the example
below:
#define NAME value
That is, if you import a project named MyProject, then the pre-processor definitions should be specified in MyProject.config
For my projects it causes the indexer to recognize this define project wide and changed the auto-complete to reflect this.
I think the initial answers are good, but they require that you manage your configuration manually whereas there're ways to let the IDE manage this for you automatically based on whether you have a release or debug configuration selected.
This bit may be redundant, but please note that this will work for you only if you are using the IDE for building. Obviously, if this is not the case, you will need a different solution.
Steps
Since pictures are worth a thousand words, here's an example of how you define a debug macro for your debug build using Qt Creator 4.3.1:
Make sure you have your Debug configuration selected;
Go to the Projects section on the left menu;
Go to the Build section
Under Build Steps, look for the Additional arguments input box;
Enter your macro definitions (e.g. DEBUG for your #ifdef DEBUGs in the code; in my case it's __CTS_DEBUG__)
The macro will now only be defined when you're using your debug config; when you choose your Release config (see step 1), it will become undefined automatically and your conditionally-compiled debug code will be removed, as shown in the pictures below, which is just what you always wanted.
Results
With the debug config selected
With the release config selected:
I wanted to specify a #define string in the .pro file, but my code ended up with the contents of the string without the quotes. I had to escape the quotes, and then escape the escapes to get one pair of quotes to last all the way to my code. This is because qmake strips off one set of escapes and some quotes, then the command line strips off the rest of them. This worked for me:
DEFINES += "VERSION=\"\\\"0.1.0\\\"\""
On Windows, this VERSION string can then be used in the .rc file to create the version stuff where Windows wants it and as well as in code for an "About" menu.
You can define some PREPROCESSOR in the Project settings in QtCreator. I do not have QtCreator here but i remember there is a tab for project configuration.
First suggestion from #Tobias Hunger's answer worked for me. I was doing this in my .pro file:
DEFINE += KEY=value
which did not work until I changed it to:
DEFINES += KEY=value
That said, if you want to do everything from the command line, an almost hybrid solution that does not use Qt Creator GUI, but does use .pro files, is described here:
https://www.linux.org/threads/c-tutorial-create-qt-applications-without-qtcreator.18409/
Using the method described in the above link, you can use qtcreator -project to produce a .pro file, then use notepad or vim or some other text editor to add the DEFINES += KEY=value line to the .pro file you just created. Then use qmake thusly: qmake <your_project>.pro, to do the Qt pre-processing, and finally just make to build an executable.
I've also heard lore of a qtcreator -D option to add preprocessor defines from the command line as described here:
https://doc.qt.io/archives/qt-4.8/qmake-variable-reference.html#defines
but I've never tried it.
Hope something works for you! Your dedication to the CLI is admirable.

Resources