c++ - CodeBlocks c++ - 不能使用线程,因为编译器不支持它

标签 c++ multithreading codeblocks

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

#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 的一个积极维护的分支)。 See here获取安装帮助。

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

关于c++ - CodeBlocks c++ - 不能使用线程,因为编译器不支持它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30837071/

相关文章:

c++ - 无法使用 boost.sh 脚本制作的 Boost 框架来制作 cpp-netlib

c++ - 线程指针的 vector ,push_back导致崩溃

c++ - 如何从基类(非抽象类)调用重载函数?

multithreading - 搞乱线程计时的工具?

c - 这个用 C 编写的程序没有给出正确的输出

c++ - 如何用参数重载 '=' 运算符?

c++ - 标准 C++ 库中的任何地方都使用了 thread::id 吗?

java - 我将我的线程设置为比其他线程更高的优先级,但 CPU 时间似乎是随机的?

c++ - 如何直接编译最初用代码:: block 编写的代码

c++ - 在您指定的 wxWidgets 目录中找不到匹配的调试配置。