std :: thread在cygwin中起作用,但在MinGw中不起作用 [英] std::thread works in cygwin but not in MinGw

查看:107
本文介绍了std :: thread在cygwin中起作用,但在MinGw中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我决定今天尝试一下c ++.我下载了MinGw及其随附的g ++编译器.我决定测试以下代码:

So I decided to give c++ a try today. I downloaded MinGw and the g++ compiler that comes with it. I decided to test the following code:

#include <iostream>
#include <thread>

int foo()
{
    std::cout << "foo" << std::endl;
}

int main()
{
    std::thread t1(foo);
    t1.join();
    std::cout << "done" << std::endl;
    return 0;
}

然后我尝试使用以下行在命令行上对其进行编译:

I then tried to compile it on the command line using the following line:

g++ -std=c++11 main.cpp

为您好世界工作的人.但是这一次,它给了我这个错误:

Which works for hello world. This time however, it gave me this error:

error: 'thread' is not a member of 'std'

我使用cygwin提供的g ++尝试了完全相同的代码,并且可以正常工作.那么,为什么它在MinGw中不起作用?是过时的还是什么?我想在cygwin终端上使用c ++ 11和c ++ 14进行编译,但是要在cygwin环境之外进行编译.

I tried the exact same code using the g++ provided by cygwin, and it works. So why doesn't it work in MinGw? Is it outdated or something? I want to compile stuff using c++11 and c++14 like on the cygwin terminal, but outside of the cygwin environment.

推荐答案

如果要使用std::thread,则需要使用posix线程支持编译MinGW-w64(或Windows上的GCC),大概是通过以下方式下载的:本机Windows线程.

MinGW-w64 (or rather GCC on windows) needs to be compiled with posix thread support if you want to use std::thread, presumably you downloaded a build with native windows threads.

查看针对 64位

Check out the mingw-builds folders targeting 64 bit or 32 bit and pick a version with posix threads. You'll also need to choose the exception handling method, if you don't have a reason to choose otherwise then stick with the GCC defaults of seh for 64 bit and dwarf for 32.

这篇关于std :: thread在cygwin中起作用,但在MinGw中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆