在Windows 10上使用C 中的线程(使用G 作为编译器)的问题
problems using threads in C++ on windows 10 (using g++ as compiler)
我正在尝试创建一个线程并将其打印到终端。我遇到了一些问题,所以我决定使用其他人制作的非常简单的代码。
#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;
}
编译器(Windows 10上的MingW32-GCC-G -BIN 8.2.0.3(给出以下错误:
.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上使用编译器MinGW 9.2.0安装OpenCV
- 如何使用 g++ 编译器和 Eigen 库在 Windows 上进行编译
- 使用编译器进行外部初始化时出错C++
- 'void *' 'void (*) (bool)' 指针使用 c ++ 编译器在 C 中编译文件的错误
- 在putty上使用g++编译器进行编译会将我的代码传递到linux服务器吗
- 使用编译器强制第一个实例
- C++对象的类型定义比较,使用编译器忽略错误的类型
- 如何在使用g++编译器时使用c样式初始化结构
- 使用 C++ 编译器设置 MATLAB
- 使用编译器安装库
- 如何在没有标准C库的情况下使用编译器内置函数
- 在 Ubuntu 上使用 g++ 编译器运行程序
- 使用编译器生成的默认初始化的成员初始化顺序
- C++ 使用编译器编译"gcc"简单文件
- 使用c++编译器运行gcov工具
- 如何使用C++(g++编译器)初始化一个无序集
- 使用g++编译器编译boost.asio
- 无法使用编译器将我的 c++ 文件制作成库
- 如何使用C++编译器/链接器/技巧来检测已分配但以后从未引用的值
- 代码块使用编译器设置中定义的DNDEBEUG进行调试