ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何用MinGW编译C std :: thread代码?

2019-10-12 19:09:23  阅读:375  来源: 互联网

标签:c c11 multithreading gcc mingw


我想用MinGW编译我的c 11项目(最近移到c 11).而且我有关于c 11代码的编译错误,例如“找不到std :: thread”.

我在gcc 5.3.0中使用了最新的MinGW(2015年12月).最后,我只想在编译我的大项目之前先编译此示例:

#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";
}

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

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

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通常没有glibc端口,该端口像在GCC中一样支持pthreading或gthreading.

为了解决这个问题,第一个解决方案是安装library of thread headers.
另一个解决方案可以与GCC编译器一起使用.

标签:c,c11,multithreading,gcc,mingw
来源: https://codeday.me/bug/20191012/1902272.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有