简体   繁体   English

std :: thread在cygwin中起作用,但在MinGw中不起作用

[英]std::thread works in cygwin but not in MinGw

So I decided to give c++ a try today. 所以我决定今天尝试一下c ++。 I downloaded MinGw and the g++ compiler that comes with it. 我下载了MinGw及其随附的g ++编译器。 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'

I tried the exact same code using the g++ provided by cygwin, and it works. 我使用cygwin提供的g ++尝试了完全相同的代码,并且可以正常工作。 So why doesn't it work in MinGw? 那么,为什么它在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. 我想像在cygwin终端上一样使用c ++ 11和c ++ 14编译东西,但是在cygwin环境之外。

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. 如果要使用std::thread ,则需要使用posix线程支持编译MinGW-w64(或Windows上的GCC),大概是您下载了带有本机Windows线程的构建。

Check out the mingw-builds folders targeting 64 bit or 32 bit and pick a version with posix threads. 查看针对64位32位的mingw-builds文件夹,并选择带有posix线程的版本。 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. 您还需要选择异常处理方法,如果您没有其他选择的理由,则坚持使用GCC的默认值seh(64位)和dwarf(32位)。

I tested this in linux (cross-compiling). 我在linux(交叉编译)中对此进行了测试。

This compiles ok, 这样可以编译,

i686-w64-mingw32-g++-posix -std=c++11 threads.cpp -lwinpthread

this does not 这不

i686-w64-mingw32-g++-win32 -std=c++11 threads.cpp -lwinpthread

The difference between the compilers is --enable-threads=win32 vs. --enable-threads=posix option used when the compilers were built. 编译器之间的区别是--enable-threads=win32与构建编译器时使用的--enable-threads=posix选项。 g++ -v should show what was used. g++ -v应该显示使用了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM