微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 G++ 在 windows (MinGW - W64) 中使用 #include <thread> 编译 C++ 应用程序?

如何解决如何使用 G++ 在 windows (MinGW - W64) 中使用 #include <thread> 编译 C++ 应用程序?

我曾尝试使用 G++ 用 #include <thread> 编译我的 C++ 文件,但它失败了。这是我的源代码 下面:

#include <iostream>
#include <thread>
#include <windows.h>

using namespace std;

void act_1()
{
    cout << "Hello World from act_1\n";
    return;
}

int main()
{
        thread th(act_1);
        th.join();
        
        ExitProcess(EXIT_SUCCESS);
}

现在,我使用了标志 -std=c++11 和 -pthreads,但它仍然无法正常工作。这是控制台输出

C:\Users\Mayukh\Desktop>g++ -std=c++11 -pthread threaded.cpp -o thread
threaded.cpp: In function 'int main()':
threaded.cpp:15:3: error: 'thread' was not declared in this scope
   thread th(act_1);
   ^~~~~~
threaded.cpp:15:3: note: 'std::thread' is defined in header '<thread>'; did you
forget to '#include <thread>'?
threaded.cpp:4:1:
+#include <thread>

threaded.cpp:15:3:
   thread th(act_1);
   ^~~~~~
threaded.cpp:16:3: error: 'th' was not declared in this scope
   th.join();
   ^~
threaded.cpp:16:3: note: suggested alternative: 'tm'
   th.join();
   ^~
   tm

请帮帮我

解决方法

我现在找到了答案,因为我使用的是 x86_x64_win32,并且它没有实现线程,所以我切换到 x86_x64_posix,它现在可以工作了。

如果有人仍然可以在 MinGW - W64 的 x86_x64_win32 中找到解决方案,请发布答案

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。