C++ 如何使用thread类编译cpp文件?

C++ 如何使用thread类编译cpp文件?,c++,multithreading,c++11,mingw,C++,Multithreading,C++11,Mingw,我正在学习使用线程,因此我尝试使用thread类运行代码,以便可以并发运行函数。然而,试图在terminals终端中编译它时,它表示线程及其对象t1没有声明 threading.cpp:16:5: error: 'thread' was not declared in this scope thread t1(task1, "Hello"); ^~~~~~ threading.cpp:21:5: error: 't1' was not declared in this s

我正在学习使用线程,因此我尝试使用thread类运行代码,以便可以并发运行函数。然而,试图在terminals终端中编译它时,它表示线程及其对象t1没有声明

 threading.cpp:16:5: error: 'thread' was not declared in this scope
     thread t1(task1, "Hello");
     ^~~~~~
 threading.cpp:21:5: error: 't1' was not declared in this scope
     t1.join();
我认为g++不支持它,但我也在它的参数中包含了支持c++11的内容

  g++ -std=c++11 threading.cpp
你知道我该怎么处理这个错误吗

(操作系统:windows,gcc版本6.3.0)

下面提供了代码(来自网站的示例):

#包括
#包括
#包括
使用名称空间std;
//我们希望在新线程上执行的函数。
无效任务1(字符串消息)
{
库特
你知道我该怎么处理这个错误吗

您的代码(coliru.com)。旧版本(如GCC 6)和
-std=c++11
也是如此。您的问题一定在其他地方:

  • 也许您正在使用一个非常旧的编译器
  • 也许没有安装C++标准库头文件?
  • <> LI>也许您的C++标准库头位于某个意外的地方?如果使用编译器或标准库的自定义安装版本,可能会发生这种情况。
请将命令和错误作为文本而不是图像发布。对于能够看到图像的人来说,这使得你的问题无法回答。<代码> G++<代码>编译C++代码,而<代码> GCC 编译C代码。@π-Youth-To-αῥεῖ 比这更复杂。所以你的问题是“为什么我不能编译这个c++11线程代码?”你有什么版本的g++呢?你可以通过运行
g++-v
#include <string>
#include <iostream>
#include <thread>

using namespace std;

// The function we want to execute on the new thread.
void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello");

    // Do other things...

    // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
    t1.join();
}