【问题标题】:MinGW error: ‘thread’ is not a member of ‘std’MinGW 错误:“线程”不是“标准”的成员
【发布时间】:2014-02-08 07:45:06
【问题描述】:

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

#include <thread>

void Func(){
  return;
}

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

这就是我得到的:

$ 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

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

【问题讨论】:

  • 如果你在 windows 上编译这个,你需要 Mingw-Builds v4.8.1 with posix-threads: sourceforge.net/projects/mingwbuilds/files/host-windows/… 你可以在 sjlj 和 seh 之间进行选择。 Seh 只是 x64,sjlj 是 x32 和 x64。
  • @CantChooseUsernames 我正在 Ubuntu 上编译它。我有来自存储库的最新 mingw; "i686-w64-mingw32-g++ --version""i686-w64-mingw32-g++ (GCC) 4.6.3"
  • @CantChooseUsernames 虽然如果我没有找到解决方案,我可以将其用作某种解决方法:通过 wine 运行 MinGW 来编译源代码。虽然很奇怪 :D 顺便问一下,我的编译器版本不支持 std::thread 的吗?
  • 我不认为 4.6.3 支持 Windows 可执行文件的每个线程。我认为 std::thread 在 4.7+ 时出现。也许更新可能会解决它。总是值得一试。
  • @CantChooseUsernames 我发现了一件奇怪的事情!我只是尝试下载 Windows 版本的 MinGW 并用 wine 运行它。而且......它的工作原理!版本号4.8.1,肯定比我之前尝试的版本要低。无论如何,它的作品!

标签: c++ multithreading c++11 mingw


【解决方案1】:

这个错误说明你使用的STL没有包含C++11的所有特性。

要在 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/

【讨论】:

  • 无法安装。它显示“无法下载repository.txt [0]”
  • 通过sudo apt-get install mingw-w64在Linux上安装mingw怎么样?
  • @NaveenKumar 看到这个:stackoverflow.com/questions/51747358/… - 我用巧克力包管理器解决了这个问题。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    在 Window 上,您可以选择使用 POSIX 线程库来构建您的应用程序。 您可以通过https://sourceforge.net/projects/pthreads4w/files/找到它

    【讨论】:

    • 有比手动设置这个库更简单的解决方案。您可以只安装 MinGW-w64(其众多发行版之一),默认情况下它应该具有多线程库功能。
    • 这个建议好像已经在in this answer这里提到了。
    猜你喜欢
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    相关资源
    最近更新 更多