12

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?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
melmi
  • 988
  • 3
  • 9
  • 27
  • Looks strange to put those settings in a "cross compiler" section (but I don't know Eclipse). The crash is repeatable if the `-pthread` flag is not present on the command line and you have other versions of libstdc++ installed. So make sure that flag is really being passed to your compiler – Mat May 01 '12 at 09:44
  • This is a runtime exception, so the code compiles correctly. Still I dont understand in which cases this exception message should occur... – Klaim May 01 '12 at 09:54
  • 1
    @Klaim: that error comes up if you link against the wrong libstdc++, which can happen if you have multiple versions of it installed and you don't pass `-pthread` (or your GCC install is borked) – Mat May 01 '12 at 09:57
  • I think `-pthread` is passed to compiler, because I put it in the same place that I put `-std=c++11`, and the code won't be compiled when I did not put `-std=c++11`. A more strange thing is that the code works correctly when I compile it from command line manually. – melmi May 01 '12 at 14:58
  • 2
    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. – Jonathan Wakely May 04 '12 at 23:11
  • Shouldn't it be -lpthread? And since it is a linker command it needs to be in a different place than -std=c++11.... – David Feurle May 05 '12 at 05:13

3 Answers3

11

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.

melmi
  • 988
  • 3
  • 9
  • 27
  • For me it was `... -> CCC C++ Linker -> Miscellaneous -> Linker flags` – gsamaras Apr 17 '15 at 20:01
  • hmm, that makes my program run & debug okey in eclipse, but the syntax checker still gives me errors when I use std::thread or thread.join. Anybody know how to solve that too? – kaefert Sep 23 '18 at 15:15
1

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:

  1. Right click on Project and select 'Properties'
  2. Go to 'C/C++ Build' > 'Settings' > (tab) 'Tool Settings'
  3. First select 'Cross G++ Compiler' > 'Miscellaneous' > 'Other flags';
    and add -pthread after -std=c++11
  4. Second select 'Cross G++ Linker' > 'Libraries';
    and add pthread (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

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
iammilind
  • 68,093
  • 33
  • 169
  • 336
1
  1. Go to Project > Properties > C/C++ General > Preprocessor include paths, etc > Providers > CDT GCC Builtin Compiler Settings and append -std=c++11 to the compiler specs.

    You can also do this for all projects going to Window > Preferences > C/C++ > Build > Settings > Discovery and append -std=c++11 to the CDT GCC Builtin Compiler Settings specs.

     ${COMMAND} ${FLAGS} -E -P -v -dD -std=c++11 "${INPUTS}"
    
  2. 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-needed
    
  3. Project 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
    
Community
  • 1
  • 1
robbycandra
  • 770
  • 7
  • 6