【问题标题】:Codeblocks C++ threading error [duplicate]代码块C++线程错误[重复]
【发布时间】:2015-06-23 07:26:46
【问题描述】:

我有一个包括线程库的问题。以下代码:

#include <string>
#include <iostream>
#include <thread>

using namespace std;

//The function we want to make the thread run.
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");

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

产生这些错误:

代码取自另一个 stackoverflow 问题的答案。我对代码块和 C++ 还很陌生,所以请向我解释我做错了什么。

【问题讨论】:

  • 好吧,确实很奇怪。我可能想知道的:你能找到标题thread 本身,然后看看那里吗?它会在 GNU/Linux 中的路径 /usr/include/c++/SomeVersion/thread 中,并且我猜在 Windows® 中的 …/include/c++/SomeVersion/thread 中的某个位置(在后一种情况下,您需要稍微搜索一下)。您必须查找该文件中某处是否有单词class thread。根据您的问题,我猜测标题以某种方式搞砸了。
  • @Hi-Angel:看起来带有 Code::Blocks 的开箱即用的 TDM 编译器是在不支持线程的情况下编译的。您必须下载不同的编译器并将其与 Code::Blocks 一起使用才能获得线程支持。
  • 如果你使用 mingw-w64 和 win32 线程,那么你需要使用meganz threading addon

标签: c++ multithreading codeblocks


【解决方案1】:

您可能没有为编译器设置正确的标志(因此它使用 c++11)。 Way of doing it in codeblocks

【讨论】:

  • 在这种情况下,编译器会抱怨未启用对 C++11 的支持。我什至只是进行了测试以确保 — 使用 -std=c++03 标志编译了他们的示例。
  • 不,这就是我运行代码时所说的一切......为什么我不能全部发布?它没有说明没有启用 C++11。甚至在我发布这个帖子之前,我就按照 Piotr 所说的方式启用了它。当我键入 #include 时,该库被识别,但每当我尝试使用线程函数时,它都不会显示
  • @SkrewCodeBlocks:您最初的问题并未显示所有内容。我用完整的输出更新了你的问题。在我之前评论的论坛讨论链接中,有一个指向 MinGW 构建的链接,该构建具有内置的 std::thread 支持。
  • @jxh 顺便说一句,我刚刚发现了一件有趣的事情:在链接引用的论坛中,人们提到缺少thread 类的问题与许可证问题有关。所以,我只是出于好奇尝试使用从 Ubuntu 存储库安装的 MinGW 来编译示例,并且……它可以工作! :/太奇怪了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多