PDA

View Full Version : How to use STL in Qt



Adi
1st April 2011, 08:56
Hi,

I am unable to find a proper way to use STL in my Qt application. Using Qt containers is currently not an option.

I tried modifying .pro file by adding:

CONFIG += stl


INCLUDEPATH += /path/to/stl (in my case /usr/include/c++/4.4)


This gives me an error:

/usr/include/c++/4.4/string:40: fatal error: bits/c++config.h: No such file or directory

I'm able to compile an ordinary C++ STL application so I assume I misconfigured something in Qt. Qt is built with STL support.

Thank you,

Adi

Zlatomir
1st April 2011, 09:23
If Qt is built with STL support than it should work by default, you don't need to add anything in .pro file or anywhere else.

But you can't use std::string instead QString, you will need to convert it to QString to use it in widgets (or any other Qt library function that takes a QString)
Check this example:

#include <QApplication>
#include <QLabel>
#include <string>
int main(int argc, char** argv) {
QApplication a(argc, argv);
std::string hello = "Hello World!";

QLabel l;
l.setText(QString::fromStdString(hello));
l.show();

return a.exec();
}

nightghost
1st April 2011, 09:49
Try to use this:



INCLUDEPATH += <stlpath>
DEPENDPATH += <stlpath>

wysota
1st April 2011, 09:53
There is no need for such things. STL should be available out of the box. If this compiles without Qt:

#include <string>

int main() { return 0;}
Then it should compile with Qt without any special treatment.

Adi
1st April 2011, 10:43
Thank you for your response.

Just tried to compile the basic Qt + STL example (as above) and it works but unfortunatelly I'm still experiencing the same problems. What I have is a quite complex Qt project which links to other non-Qt code written in standard C++ and STL.

My development structure consists of following folders:

Development
|
-------Qt project folder
|
-------Some other folder
|
--------- STL related code folder

So problem occurs when header with STL related code is included in Qt app. Important to notice is that header is NOT in the same folder as Qt related files. It seems that Qt doesn't recognize STL from such scope:

In file included from qtapp.h:12,

stlRelatedCode.h:17: fatal error: string: No such file or directory

I'm assuming that it could be something with given folder structure. I'll try with moving STL code in the same folder but that will really mess up the code structure :/

high_flyer
1st April 2011, 11:04
Just tried to compile the basic Qt + STL example (as above) and it works
This means, that your compiler knows where STL headers are, becuase the example includes an stl header.
So STL location seems NOT to be your problem.
What do you mean by "STL related code"?
It seems that you mean by that code that uses STL, rather than STL header.
If so, your problem seems to be that you project configurations doesn't have the correct paths to all the headers you are using (but which are NOT STL headers)

Adi
1st April 2011, 11:17
Yes, I was thinking on code that is just using STL library (i.e. std::string) and not on the STL headers.

That STL code is packed as .a lib so all I need to do to use is just set


INCLUDEPATH += /path/to/lib/headers
LIBS += /path/to/lib

which I've already done.

Can't really find the source of the problem :/

But nonetheless thank you for cooperating :)

high_flyer
1st April 2011, 11:32
There is no magic here.
It looks like your '/path/to/lib/headers' is wrong, even though you think it is right.
There is no reason why your compiler can find some headers and not others, apart from it not having the correct path to them.

Adi
1st April 2011, 12:31
It seems that the whole Eclipse project was somehow messed up as it compiles successfully as soon as I created a new project from the start with same settings.

Thank you all for your help.

Adi