gpt4 book ai didi

c++ - 在 Windows 10 上使用 C++ 线程的问题(使用 g++ 作为编译器)

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:14:10 26 4
gpt4 key购买 nike

我正在尝试创建一个线程并让它向终端打印一些内容。我遇到了一些问题,所以我决定采用别人制作的这段非常简单的代码,当我编译它时,我得到了下面列出的错误,但网上的其他人似乎没有问题运行它。

#include <iostream>
#include <thread>

using namespace std;

void hello_world()
{
cout << "Hello from thread!\n";
}

int main()
{
thread threadobj1(hello_world);
threadobj1.join();
return 0;
}

编译器(mingw32-gcc-g++-bin 8.2.0.3 on windows 10)给出以下错误:

.\multiT.cpp: In function 'int main()':
.\multiT.cpp:13:5: error: 'thread' was not declared in this scope
thread threadobj1(hello_world);
^~~~~~
.\multiT.cpp:13:5: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?.\multiT.cpp:3:1:
+#include <thread>

.\multiT.cpp:13:5:
thread threadobj1(hello_world);
^~~~~~
.\multiT.cpp:14:5: error: 'threadobj1' was not declared in this scope
threadobj1.join();
^~~~~~~~~~
.\multiT.cpp:14:5: note: suggested alternative: 'thread_local'
threadobj1.join();
^~~~~~~~~~
thread_local

我希望有人能帮我弄清楚为什么这对我不起作用,错误说我应该包括 ,但我显然已经这样做了,所以我现在有点迷茫了。我已经尝试安装“mingw32-pthreads-w32-dev”包,因为它们没有安装,但没有任何区别。我还向编译器添加了以下参数:

g++ -std=c++14 -pthread .\multiT.cpp

最佳答案

对于处理此问题的其他人:最简单的解决方案是下载 mingw-64并改用他们的编译器。

然后使用 -std=gnu++11-std=c++11 参数启用对 ISO C++ 2011 标准的支持并扩展支持线程(请注意:此支持目前是实验性的,尽管到目前为止还没有给我带来任何问题)。

关于c++ - 在 Windows 10 上使用 C++ 线程的问题(使用 g++ 作为编译器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56092943/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com