【记录】在Windows上使用MinGW.Thread.h等文件测试C++11多线程;及异常解方:error To use the MinGW-std-threads library;

环境

Window10, MinGW。
注意:
如果要使用C++11版本的编译器,需要在下载的MinGW中加入一些头文件,因为MinGW提供的编译器目前缺少有关Thread的头文件【1】。你可以在Gitee或者Github上搜索关键词mingw-std-threads,相关资源都可以使用。我使用的提取地址如下:

https://gitee.com/CreateMe/mingw-std-threads

在此感谢资源提供者!
将包中的头文件放入MInGW的如下位置,

C:\MinGW\lib\gcc\mingw32\6.3.0\include\c++

结果如下。
在这里插入图片描述

步骤

  1. 编写代码
    测试代码参考此位置最下方第一条笔记,

https://www.runoob.com/cplusplus/cpp-multithreading.html

注意:

  • 用我们刚刚下载的<mingw.thread.h>代替<thread.h>

问题:

  • error To use the MinGW-std-threads library, you will need to define the macro _WIN32_WINNT to be 0x0501 (Windows XP) or higher.
    原因是,宏_WIN32_WINNT是用于定义Windows和Internet Exporeer版本号的,,用于区分不用版本的函数和参数。在你编译程序时会根据不同的版本系统号,链接不同的函数和参数。【2】
    你可以通过右键点击并查看这个宏的内容,可以有
#define _WIN32_WINNT_NT4	0x0400		/* Windows NT4 */
#define _WIN32_WINNT_NT4E	0x0401		/* Windows NT4E */
#define _WIN32_WINNT_NT4SP3	0x0403		/* Windows NT4 + ServicePack 3 */
#define _WIN32_WINDOWS_95	0x0400		/* Windows 95 */
#define _WIN32_WINDOWS_98	0x0410		/* Windows 98 */
#define _WIN32_WINDOWS_ME	0x0490		/* Windows Millenium Edition */
#define _WIN32_WINNT_WIN2K	0x0500		/* Windows 2000 */
#define _WIN32_WINNT_WINXP	0x0501		/* Windows XP */
#define _WIN32_WINNT_WS03	0x0502		/* Windows Server 2003 */
#define _WIN32_WINNT_WIN6	0x0600		/* Alias for Windows Vista */
#define _WIN32_WINNT_VISTA	0x0600		/* Windows Vista */
#define _WIN32_WINNT_WS08	0x0600		/* Windows Server 2008 */
#define _WIN32_WINNT_LONGHORN	0x0600		/* Alias for Windows Vista */
#define _WIN32_WINNT_WIN7	0x0601		/* Windows 7 */
#define _WIN32_WINNT_WIN8	0x0602		/* Windows 8 */
#define _WIN32_WINNT_WINBLUE	0x0603		/* Windows 8.1 */

这里需要对宏_WIN32_WINNT的定义大于0x0500,所以可以任选0x0500之后的Win版本。我修改为如下,

#ifndef _WIN32_WINNT
# ifdef WINVER
#  define _WIN32_WINNT WINVER
# else
#  ifdef _WARN_DEFAULTS
#   warning "Assuming default setting of _WIN32_WINNT_WIN2K for _WIN32_WINNT"
#  endif
#  define _WIN32_WINNT **_WIN32_WINNT_WINBLUE**
# endif
#endif

再运行即可。

参考:

  1. C++11 thread类在windows上无法使用。std 没有成员 thread、thread not member of std
  2. VC中的_WIN32_WINDOWS、_WIN32_WINNT、WINVER等宏定义
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值