PDA

View Full Version : How can i use thread from C++11 in qt creator



Hossein
3rd September 2012, 21:43
Hello All.c
I have the latest mingw version (4.7.1) and i have it set-up and successfully ran a sample file (which i post below)in CodeBlocks using the mention compiler.
Now i want to use std::thread in Qt. when i try to compile this sample:

#include <QCoreApplication>
#include <iostream>
#include <vector>
#include <thread>


void func(int tid)
{
std::cout << "Launched by thread " << tid << std::endl;
}



int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

std::vector<std::thread> th;

int nr_threads = 10;

for (int i = 0; i < nr_threads; ++i)
{
th.push_back(std::thread(func,i));
}

for(auto &item : th)
{
item.join();
}


return a.exec();
}

I get an error saying :

C:\Users\Master\Documents\Qt\ThreadCpp11\main.cpp: 4: error: C1083: Cannot open include file: 'thread': No such file or directory.
What other steps are necessary in order to get this to work in QtCreator?(QT4.8.2)
here are some pics showing my settings :
http://upload.ustmb.ir/uploads/39bb1.jpg
http://upload.ustmb.ir/uploads/39bb2.jpg

Thanks in advance

ChrisW67
3rd September 2012, 23:20
This is between you and your C++ compiler: not Qt.

The error message (http://msdn.microsoft.com/en-us/library/et4zwx34.aspx) you have given is from a Microsoft compiler, not MingW. AFAICT (here (http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx) for example) there is no C++11 thread support in MSVC10. Consequently, there's no <thread> header to include with the Microsoft compiler you are using.

If you do use MingW/g++ 4.7 then you will need to add at least "-std=c++11" to your compiler options (using QMAKE_CXXFLAGS) in order to invoke the experimental C++11 support (http://gcc.gnu.org/gcc-4.7/cxx0x_status.html).

Hossein
4th September 2012, 05:55
This is between you and your C++ compiler: not Qt.

The error message (http://msdn.microsoft.com/en-us/library/et4zwx34.aspx) you have given is from a Microsoft compiler, not MingW. AFAICT (here (http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx) for example) there is no C++11 thread support in MSVC10. Consequently, there's no <thread> header to include with the Microsoft compiler you are using.

If you do use MingW/g++ 4.7 then you will need to add at least "-std=c++11" to your compiler options (using QMAKE_CXXFLAGS) in order to invoke the experimental C++11 support (http://gcc.gnu.org/gcc-4.7/cxx0x_status.html).

Thanks.But i believe that i set the QtCreators compiler to Mingwgcc 4.7.1 and they are evident in the pictures i posted.I know that VS2010 doesnt support C++11,butMingw(gcc port on windows ) does.Would you please be more specific in terms of how i can configure QtCreator for such a task.I believe i did the conventional configuration though

----------
Edited:
I noticed the VC11 compiler is still active , How can i set Mingw as the default compiler?apparently choosing Mingw from Auto-Detected list from ToolChain tab in QtCreator is not the case because it just doesnt work!:-/:confused:

ChrisW67
4th September 2012, 22:29
Select tool chain
Click Apply or OK
Do a complete rebuild of the project including running qmake. If you do not rerun qmake then the old Makefile, full of MSVC commands, will still be present.


You should do something like this in the PRO file:


win32-g++ {
QMAKE_CXXFLAGS += -std=c++11
}

Hossein
5th September 2012, 14:56
Select tool chain
Click Apply or OK
Do a complete rebuild of the project including running qmake. If you do not rerun qmake then the old Makefile, full of MSVC commands, will still be present.


You should do something like this in the PRO file:


win32-g++ {
QMAKE_CXXFLAGS += -std=c++11
}


still not a go:(
Here are the screenshots showing what i have done. :
http://upload.ustmb.ir/uploads/e2f0-pro-file.png
http://upload.ustmb.ir/uploads/e2f0Toolchain.png
http://upload.ustmb.ir/uploads/e2f0Untitled2.png
what am i missing?

ChrisW67
5th September 2012, 23:23
The Tools Options panel is for configuring what tool chains are available to Qt Creator, and which one to use if you open a previously unused project.

The Projects, Build Settings panel is where you set the tool chain being used by a given project.
8189
Your project is using the Microsoft tool chain.

Hossein
6th September 2012, 08:01
The Tools Options panel is for configuring what tool chains are available to Qt Creator, and which one to use if you open a previously unused project.

The Projects, Build Settings panel is where you set the tool chain being used by a given project.
8189
Your project is using the Microsoft tool chain.
Thank you I tried to add Mingw tool chain through the same pane you just showed to me(using Manage button) , when i click apply and then ok to either select MingW(x86) from the tool chains list or a manual Mingw tool-chain, nothing is added to the tool-chain ComboBox inside projects/build settings!so that i can change the tool-chain to mingw from visual C++ compiler
It is driving me absolutely crazy.

ChrisW67
6th September 2012, 23:16
It is in the Qt Creator manual under Managing Projects, Configuring Projects, Specifying Build Settings (http://doc.qt.nokia.com/qtcreator-2.5/creator-build-settings.html). A project can have many build configurations. Yours current has two, one debug and one release, bot MSVC. You want to add another two for MingW and possibly remove the MSVC ones. Click the "Add" button at the top of the Build Settings panel to add more allowable build configurations for this project. Then select the one you want to use when you build.