问题
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.
Any comments?
回答1:
The problem was solved by the comment of Jonathan Wakely.
I added -pthread to C/C++ Build -> Settings -> Tool Settings -> Cross G++ **Linker** -> Miscellaneous -> Other Flags and the program worked correctly.
Thank you Jonathan.
回答2:
To work C++11 std::thread in Eclipse, one needs to give -pthread option while compiling. However that's not enough. In my Ubuntu 14.04, with Eclipse Kepler and g++4.9 below makes it work:
- Right click on Project and select 'Properties'
- Go to 'C/C++ Build' > 'Settings' > (tab) 'Tool Settings'
- First select 'Cross G++ Compiler' > 'Miscellaneous' > 'Other flags';
and add-pthreadafter-std=c++11 - Second select 'Cross G++ Linker' > 'Libraries';
and addpthread(which is equivalent to command line-lpthread)
Finally re-compile the project; the error should go.
Also remember that if you use, std::thread then its object must be join() somewhere. Else you may get below runtime error:
terminate called without an active exception
回答3:
Go to Project > Properties > C/C++ General > Preprocessor include paths, etc > Providers > CDT GCC Builtin Compiler Settings and append
-std=c++11to the compiler specs.You can also do this for all projects going to Window > Preferences > C/C++ > Build > Settings > Discovery and append
-std=c++11to the CDT GCC Builtin Compiler Settings specs.${COMMAND} ${FLAGS} -E -P -v -dD -std=c++11 "${INPUTS}"Project Properties > C/C++ Build > Settings > Tool Settings > GCC C++ Compiler > Miscellaneous > Other flags, add
-pthread -std=c++11 -Wl,--no-as-needed:-c -fmessage-length=0 -pthread -std=c++11 -Wl,--no-as-neededProject Properties > C/C++ Build > Settings > Tool Settings > GCC C++ Linker > Miscellaneous > Linker flags, add
-pthread -std=c++11 -Wl,--no-as-needed-pthread -std=c++11 -Wl,--no-as-needed
来源:https://stackoverflow.com/questions/10395936/how-to-make-cdt-eclipse-work-with-c11-threads