0
votes

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?

1

1 Answers

3
votes

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