问题描述
所以我决定今天尝试一下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位或 32位,然后选择带有posix线程的版本.您还需要选择异常处理方法,如果您没有其他选择的理由,请坚持使用GCC的默认值seh(64位)和dwarf(32位).
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中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!