【问题标题】:CodeBlocks c++ - Can't use Thread because the compiler doesn't support itCodeBlocks c++ - 不能使用线程,因为编译器不支持它
【发布时间】:2015-06-15 03:49:00
【问题描述】:

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

#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 &lt;thread&gt;时,我得到了这个按摩 -

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

如何解决这个问题?

【问题讨论】:

  • 您的错误信息正在为您提供解决方案...
  • 您安装了支持旧版 C++ 的 CodeBlock。要升级,谷歌“codeblocks C++ 11”。例如:StackOverflow: How can I add C++11 support to code::blocks compiler?YouTube: How to add C++11 support to code::blocks compiler
  • @paulsm4 CodeBlocks 不是编译器,如果此消息出现在 libstdc++ 中,他使用的是 确实 支持 C++11 的 gcc 版本它们是一起发货的。
  • @o11c:我知道 Codeblocks 是一个IDE(我更喜欢Eclipse/CDT,谢谢)。关键是,他需要在 Google 上获取 特定于 Codeblocks 的更新程序。这可能与更新 Eclipse 或更新 MinGW 的过程不同,也可能没有不同……或者,就此而言,更新命令行 gcc/g++ 工具链。
  • @paulsm4 除了绝对不需要升级。只需更改项目设置。

标签: c++ multithreading codeblocks


【解决方案1】:

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

GNU glibc 不支持 Windows 线程(其开发团队不关心 Windows),因此您必须使用 MinGW pthread 构建,或使用附加组件。

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

但是,您的错误消息表明您安装了相当旧的 g++ 版本,因此我建议升级到 Mingw-w64(Mingw 的一个积极维护的分支)。 See here 获取安装帮助。

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

【讨论】:

  • 在 Code::Blocks 中,应该如何(在构建选项的哪个部分)添加-std=c++11
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
  • 2011-12-23
  • 2013-02-06
  • 2017-05-20
  • 2014-07-29
  • 1970-01-01
相关资源
最近更新 更多