Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » How to make CDT/Eclipse work with C++11 threads?
How to make CDT/Eclipse work with C++11 threads? [message #869734] Fri, 04 May 2012 05:41 Go to next message
Mohammad Elmi is currently offline Mohammad Elmi
Messages: 3
Registered: May 2012
Junior Member
Hi,

My first post in the CDT forum!

I tried to test an example of C++11 threads in Eclipse. But I got this message when running the program:
terminate called after throwing an instance of 'std::system_error' what(): Operation not permitted'

My system: ubuntu + gcc 4.7

Program:

#include <iostream>
#include <thread>

void worker()
{
    std::cout << "hello from worker" << std::endl;
}

int main(int argc, char **argv)
{
    std::thread t(worker);
    t.join();
}
...and yes, I put -std=c++11 and -pthread inside C/C++ Build -> Settings -> Tool Settings -> Cross G++ Compiler -> Miscellaneous -> Other Flags.

How can I fix this?
Re: How to make CDT/Eclipse work with C++11 threads? [message #869802 is a reply to message #869734] Fri, 04 May 2012 10:58 Go to previous messageGo to next message
Axel Mueller is currently offline Axel Mueller
Messages: 1960
Registered: July 2009
Senior Member
Did you link against pthread library?
http://stackoverflow.com/questions/8649828/what-is-wrong-with-this-use-of-stdthread


Before you ask
- search this forum
- see the FAQ http://wiki.eclipse.org/CDT/User/FAQ
- google
Re: How to make CDT/Eclipse work with C++11 threads? [message #869909 is a reply to message #869802] Fri, 04 May 2012 18:14 Go to previous messageGo to next message
Mohammad Elmi is currently offline Mohammad Elmi
Messages: 3
Registered: May 2012
Junior Member
Axel Mueller wrote on Fri, 04 May 2012 06:58
Did you link against pthread library?

Yes:
Quote:
...and yes, I put -std=c++11 and -pthread inside C/C++ Build -> Settings -> Tool Settings -> Cross G++ Compiler -> Miscellaneous -> Other Flags.


About the link: The only thing that thread suggests is to use -lpthread instead of -pthread. I examined it but I still get the same error. A strange thing with that thread is that his problem was solved by changing the order of parameters.

Another thing is that I have no problem when compiling this program in command line like this:
g++ -std=c++11 -pthread test.cc
Re: How to make CDT/Eclipse work with C++11 threads? [message #869958 is a reply to message #869909] Sat, 05 May 2012 07:52 Go to previous message
Mohammad Elmi is currently offline Mohammad Elmi
Messages: 3
Registered: May 2012
Junior Member
I asked the same question at stackoverflow, and here is the answer: (the forum doesn't let me to post links)

That error definitely happens when -pthread isn't used (even if you only have one libstdc++ installed). Make sure -pthread is used for the compiler command and the linker command. Otherwise libpthread.so won't be linked to and threads cannot be launched at runtime.

Then I added -pthread to C/C++ Build -> Settings -> Tool Settings -> Cross G++ **Linker** -> Miscellaneous -> Other Flags and the program worked correctly.
Previous Topic:Included namespace's content not recognized
Next Topic:Problem reading text file using argc and argv
Goto Forum:
  


Current Time: Thu Oct 02 00:24:40 GMT 2014

Powered by FUDForum. Page generated in 0.03943 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software