【问题标题】:minGW (with g++ 4.7.1) not compiling simple C++11 code (under WinVista)minGW(使用 g++ 4.7.1)不编译简单的 C++11 代码(在 WinVista 下)
【发布时间】:2013-04-26 02:03:57
【问题描述】:

我做了功课,并在这里和网上寻找答案。下面的简单代码没有编译:

#include <thread>
#include <iostream>

void hello()
{
    std::cout << "Hello from thread " << std::endl;
}

int main()
{
    std::thread t1(hello);
    t1.join();
    return 0;
}

这是非常简单的代码,但我收到以下错误:

Thread_Cpp11_002.cpp: In function 'int main()'
Thread_Cpp11_002.cpp:14:5: error: 'thread' is not a member of 'std'
Thread_Cpp11_002.cpp:14:17: error: expected ';' before 't1'
Thread_Cpp11_002.cpp:15:5: error: 't1' was not declared in this scope

我已经尝试了很多东西,但都没有奏效:

  1. 声明t1 使用thread 而不是std::thread
  2. 使用 g++ 4.8.0 编译
  3. 单独和一起使用以下标志:-pthread-std=gnu++11-std=c++0x-std=c++11...

所以,我认为这个问题值得在这里发布。

这些编译是通过命令行完成的。让它工作后,我将尝试使用 Code::Blocks 12.11,它在 C++98 上运行良好。

记住,我使用的是 Windows Vista。

【问题讨论】:

  • 没有关于缺少#include 的错误?
  • 你传递给编译器的参数是什么?它应该有 -std=c++11 。
  • @zneak,我也尝试使用“thread.h”或进行更改,在这两种情况下,编译都没有启动(没有这样的文件)。
  • @Elazar,是的,我通过了:g++ -std=c++11 -o ...
  • @ajmacedo 和 -pthread ?

标签: c++ multithreading c++11 windows-vista mingw32


【解决方案1】:

这是一些 MinGW 版本的众所周知的问题(只需在您的 favorite engine 中搜索 mingw thread)。如果您需要线程支持,则必须使用其他库(例如 boost 或本机 Windows API)或其他构建。

【讨论】:

  • 我已经尝试了 3 个小时......但失败了!我快要放弃了。我阅读了各种相关的帖子,似乎我必须越来越多地研究编译器,而我只是想尝试 C++11。不幸的是,我使用的不是具有 C++11 功能的 MSVC 2012 Express 的 Vista。
  • @ajmecedo:这就是@jerry 提到boost 的原因。 boost 为 Visual Studio 2010 等 pre c++-11 编译器提供了thread
  • @ajmacedo 我不能保证它,但显然sourceforge.net/projects/mingwbuilds/files/host-windows/… 支持 C++11 线程。如果你选择使用 boost,它的网站是boost.org
猜你喜欢
  • 1970-01-01
  • 2012-05-08
  • 2016-02-28
  • 2014-10-17
  • 2022-01-11
  • 2015-11-05
  • 2016-11-09
  • 2012-12-02
相关资源
最近更新 更多