简体   繁体   中英

Eclipse c++11 thread support not working

I have followed the recommendations of other answers on SO, but still Eclipse will not build my threaded application:

#include <thread>
#include <iostream>

void func() {
  std::cout << "Hello thread!" << std::endl;
}

int main() {
  std::thread t(func);
  t.join();
  return 0;
}

Here are the commands Eclipse is using (emphasis mine):

g++ -O0 -g3 -Wall -c -fmessage-length=0 -pthread -std=c++11 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"

g++ -pthread -std=c++11 -o "sandbox" ./main.o

As you can see, I added -pthread and -std=c++11. Still, Eclipse complains at run-time:

terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted

Also, building manually fails:

 $ g++ main.cpp -o main.out -pthread -std=c++11

 $ ./main.out 
 terminate called after throwing an instance of 'std::system_error'
   what():  Enable multithreading to use std::thread: Operation not permitted
Aborted

What else can I do?

The answer was to add the following to two locations:

-Wl,--no-as-needed

It should be added to:

1) Project Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Miscellaneous -> Other flags

Example: -c -fmessage-length=0 -pthread -std=c++11 -Wl,--no-as-needed

2) Project Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Miscellaneous -> Linker flags

Example: -pthread -std=c++11 -Wl,--no-as-needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM