C++ MinGW 错误:“线程”不是“标准”的成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21211980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:32:38  来源:igfitidea点击:

MinGW error: ‘thread’ is not a member of ‘std’

c++multithreadingc++11mingw

提问by Hi-Angel

I'm trying to cross-compile for Windows a simple application:

我正在尝试为 Windows 交叉编译一个简单的应用程序:

#include <thread>

void Func(){
  return;
}

int main(){
  std::thread thr1(Func);
  thr1.detach();
  return 0;
}

And that's what I get:

这就是我得到的:

$ i686-w64-mingw32-g++ -static-libstdc++ -static-libgcc -pipe -g -std=c++0x ./threadstutor.cpp 
./threadstutor.cpp: In function ‘int main()':
./threadstutor.cpp:8:3: error: ‘thread' is not a member of ‘std'
./threadstutor.cpp:8:15: error: expected ‘;' before ‘thr1'
./threadstutor.cpp:9:3: error: ‘thr1' was not declared in this scope

Actually, this code have no such problem if compile with g++ for Ubuntu; but I need to cross-compile for Windows, and here I'm stuck.

其实这个代码在Ubuntu下用g++编译没有这个问题;但我需要为 Windows 进行交叉编译,而我被卡住了。

采纳答案by Sergey K.

This error means that the STL you are using does not contain all the features of C++11.

此错误意味着您使用的 STL 不包含 C++11 的所有功能。

To access C++11 threads in Windows, you will need a build of Mingw with posix-threads. Here you can find Mingw-Builds v4.8.1: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.8.1/64-bit/threads-posix/sjlj/

要在 Windows 中访问 C++11 线程,您需要使用 posix-threads 构建 Mingw。在这里你可以找到 Mingw-Builds v4.8.1:http: //sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.8.1/64-bit/threads-posix/sjlj/

回答by Alexander Vassilev

There is already a better option: https://github.com/meganz/mingw-std-threadsThis is a lightweight win32 native implementation of the most used threading and synchronization C++11 classes for MinGW. These are implemented in a header-only library that can co-exist with the system libs. It supports Windows XP as well, which does not have a direct analog of conditional variables.

已经有更好的选择:https: //github.com/meganz/mingw-std-threads这是MinGW最常用的线程和同步C++11类的轻量级win32本机实现。这些是在可以与系统库共存的仅头文件库中实现的。它也支持 Windows XP,它没有条件变量的直接模拟。