CodeBlocks 无法使用 Thread,因为编译器不支持它

CodeBlocks Can't use Thread because the compiler doesn't support it

本文关键字:因为 编译器 不支持 Thread CodeBlocks      更新时间:2023-10-16

首先,我想创建一个程序,将用户输入时间的时间计算为零,这就是代码 -

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
#include <thread>
using namespace std;
//  sleep(5000);
int runTimer = 1;
int seconds;
int hoursLeft;
int minutesLeft;
int secondsCount=0;
void timeLeft ()
{
    hoursLeft = seconds/3600;
    minutesLeft = seconds/60 - hoursLeft*60;
}
void timer ()
{
    while(runTimer=1)
    {
         if (secondsCount == 60)
    {
    timeLeft();
    cout << "The Amount of time left is: " << hoursLeft << " hours and " << minutesLeft << " minutes left." << endl;
    secondsCount=0;
    }
    secondsCount++;
    seconds--;
    Sleep(1000);
    timer();
    }
}
int main()
{
    // introduction and time picking
    cout << "Welcome to my Timer - Please set the amount of hours and than minutes you want the timer to run" << endl;
    double requestedHours, requestedMinutes;
    cin >> requestedHours;
    cin >> requestedMinutes;
    double requestedSeconds = requestedHours*3600 + requestedMinutes*60;
    seconds = requestedSeconds;
    cout << "Timer Started";
    timer();
}

但是我想添加一个功能,用户可以在其中键入一个单词或字母来暂停程序,(经过一番整理后,我发现了线程) - 但是当我添加#include <thread>时,我得到了这个按摩 -

"#error This file requires compiler and library support for the 
ISO C++ 2011 standard. This support is currently experimental, and must be 
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif" 

如何解决这个问题?

你似乎在Windows中使用g++,所以我认为它是Code::Blocks附带的MinGW的一个风格。

GNU glibc不支持Windows线程(它的开发团队不关心Windows),所以你必须使用MinGW pthread构建,或者使用附加组件。

首先,您应该将-std=c++11添加到构建选项中。

但是,您的错误消息表明您已经安装了相当旧版本的g ++,因此我建议升级到Mingw-w64(Mingw的积极维护分支)。有关安装帮助,请参阅此处。

有关各种版本的 MinGW 中的线程支持的更多信息,请参阅此线程。我成功地将 Mingw-w64 与 Win32 线程和 Code::Blocks 中的 meganz 插件一起使用。