簡體   English   中英

Eclipse C ++ 11線程支持不起作用

[英]Eclipse c++11 thread support not working

我遵循了SO上其他答案的建議,但是Eclipse仍不會構建線程應用程序:

#include <thread>
#include <iostream>

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

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

以下是Eclipse使用的命令(重點是我的命令):

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“沙盒” ./main.o

如您所見,我添加了-pthread和-std = c ++ 11。 盡管如此,Eclipse在運行時仍然抱怨:

拋出'std :: system_error'實例后調用終止
what():啟用多線程以使用std :: thread:不允許進行操作

此外,手動構建失敗:

 $ 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

我還可以做些什么?

答案是將以下內容添加到兩個位置:

-Wl,--no-as-needed

它應該添加到:

1)項目屬性-> C / C ++構建->設置->工具設置-> GCC C ++編譯器->其他->其他標志

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

2)項目屬性-> C / C ++構建->設置->工具設置-> GCC C ++鏈接器->其他->鏈接器標志

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM