How to make CDT/Eclipse work with C++11 threads?

后端 未结 3 2430
予麋鹿
予麋鹿 2021-02-20 01:15

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 \'

3条回答
  •  -上瘾入骨i
    2021-02-20 01:51

    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

提交回复
热议问题