如何用MinGW编译C ++ std :: thread代码? [英] How to compile C++ std::thread code with MinGW?

查看:1056
本文介绍了如何用MinGW编译C ++ std :: thread代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用MinGW编译我的c ++ 11项目(最近移植到c ++ 11)。我已经编译了有关c ++ 11代码的错误,比如std :: thread not found。

我使用了最后一个MinGW和gcc 5.3.0(2015年12月) 。最后,我只想编译这个例子来编译我的大项目:

  #include< iostream> 
#include< thread>
#include< chrono>

void foo()
{
//模拟昂贵的操作
std :: this_thread :: sleep_for(std :: chrono :: seconds(1));

$ b void bar()
{
//模拟昂贵的操作
std :: this_thread :: sleep_for(std :: chrono :: seconds (1));
}

int main()
{
std :: cout<< 开始帮助...... \\\
;
std :: thread helper1(foo);

std :: cout<< 开始第二个帮手... \\\
;
std :: thread helper2(bar);

std :: cout<< 等待助手完成......<<的std :: ENDL;
helper1.join();
helper2.join();

std :: cout<< 做\\\
!;
}

(来源: http://en.cppreference.com/w/cpp/thread/thread/join

我试过g ++ -std = c ++ 11 main.cpp和g ++ main.cpp -std = c ++ 0x,但我总是遇到以下错误:

  main.cpp:在函数'void foo()'中:
main.cpp:8:10:error:'std :: this_thread'尚未被声明
std :: this_thread :: sleep_for(std :: chrono :: seconds(1));
^
main.cpp:在函数'void bar()'中:
main.cpp:14:10:错误:'std :: this_thread'未被声明
的std :: this_thread :: sleep_for(标准::计时::秒(1));
^
main.cpp:在函数'int main()'中:
main.cpp:20:5:error:'thread'不是'std'的成员
std :: thread helper1(foo);
^
main.cpp:23:5:error:'thread'不是'std'的成员
std :: thread helper2(bar);
^
main.cpp:26:5:错误:'helper1'未在此范围内声明
helper1.join();
^
main.cpp:27:5:错误:'helper2'未在此范围内声明
helper2.join();
^


解决方案

MinGW大多没有支持pthreading或gthreading的glibc端口,如GCC。



为了解决这个问题,第一个解决方案可以安装线程头文件库
另一个解决方案可以使用GCC编译器。


I would like to compile my c++11 project (recently moved to c++11) with MinGW. And I have compiling errors about c++11 code like "std::thread not found".

I used the last MinGW with gcc 5.3.0 (December 2015). A the end, I would like only to compile this example before to compile my big project :

#include <iostream>
#include <thread>
#include <chrono>

void foo()
{
    // simulate expensive operation
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

void bar()
{
    // simulate expensive operation
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main()
{
    std::cout << "starting first helper...\n";
    std::thread helper1(foo);

    std::cout << "starting second helper...\n";
    std::thread helper2(bar);

    std::cout << "waiting for helpers to finish..." << std::endl;
    helper1.join();
    helper2.join();

    std::cout << "done!\n";
}

(source : http://en.cppreference.com/w/cpp/thread/thread/join)

I tried "g++ -std=c++11 main.cpp" and "g++ main.cpp -std=c++0x" but I have always those following errors :

main.cpp: In function 'void foo()':
main.cpp:8:10: error: 'std::this_thread' has not been declared
     std::this_thread::sleep_for(std::chrono::seconds(1));
          ^
main.cpp: In function 'void bar()':
main.cpp:14:10: error: 'std::this_thread' has not been declared
     std::this_thread::sleep_for(std::chrono::seconds(1));
          ^
main.cpp: In function 'int main()':
main.cpp:20:5: error: 'thread' is not a member of 'std'
     std::thread helper1(foo);
     ^
main.cpp:23:5: error: 'thread' is not a member of 'std'
     std::thread helper2(bar);
     ^
main.cpp:26:5: error: 'helper1' was not declared in this scope
     helper1.join();
     ^
main.cpp:27:5: error: 'helper2' was not declared in this scope
     helper2.join();
     ^

解决方案

MinGW mostly does not have a port of glibc which supports pthreading or gthreading like in GCC.

For solving this, the first solution can be installing library of thread headers. The other solution can be working with GCC compiler.

这篇关于如何用MinGW编译C ++ std :: thread代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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