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

Posted

技术标签:

【中文标题】CodeBlocks c++ - 不能使用线程,因为编译器不支持它【英文标题】:CodeBlocks c++ - Can't use Thread because the compiler doesn't support it 【发布时间】: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”。例如:***: 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 除了绝对不需要升级。只需更改项目设置。 【参考方案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

以上是关于CodeBlocks c++ - 不能使用线程,因为编译器不支持它的主要内容,如果未能解决你的问题,请参考以下文章

CodeBlocks环境搭建及创建第一个C++程序

Shell(C++实现,CodeBlocks+GCC编译)

CodeBlocks

Ubuntu 16.04 安装C++开发平台CodeBlocks

Ubuntu 16.04 安装C++开发平台CodeBlocks

Socket服务端和客户端(C++,CodeBlocks+GCC编译)