2

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

#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。

4

1 回答 1

1

这是一些 MinGW 版本的众所周知的问题(只需mingw thread在您最喜欢的引擎中搜索)。如果您需要线程支持,则必须使用另一个库(例如 boost 或本机 Windows API)或不同的构建。

于 2013-04-26T02:37:50.523 回答