为什么我的编译器无法识别#include< thread>(c ++)? [英] Why doesn't my compiler recognize #include <thread> (c++)?

查看:42
本文介绍了为什么我的编译器无法识别#include< thread>(c ++)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将其编写为多线程示例的简化版本,以便对此有所了解,但在编译时会遇到一些问题.我的编译器说 thread 不是 std 的成员,并提示我将 #include< thread> 添加到我的代码中,即使我有已经这样做了.到目前为止,我还没有发现任何类似的问题,但是我怀疑这与我的编译方式有关,因为我的代码与示例代码非常相似.

I wrote this as a simplified version of a multithreading example to get a feel for it, but am running into some issues when compiling. My compiler says that thread is not a member of std and prompts me to add #include <thread> to my code even though I have already done so. I've been unable to find any similar problems so far, but I suspect that it is an issue with how I'm compiling it because my code is very similar to the example code.

#include <iostream>
#include <thread>

void doWork () {
    std::cout << "Working...\n";
}

int main () {
    std::thread worker(doWork);
    
    work.join();
    std::cout << "Finished\n";
    
    return 0;
}

我的编译器是MinGW g ++ 9.2.0
我使用 g ++ main.cpp -o main 进行编译,它给了我这些错误:

My compiler is MinGW g++ 9.2.0
I compiled with g++ main.cpp -o main and it gave me these errors:

main.cpp: In function 'int main()':
main.cpp:9:7: error: 'thread' is not a member of 'std'
    9 |  std::thread worker(doWork);
      |       ^~~~~~
main.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
    2 | #include <thread>
  +++ |+#include <thread>
    3 |
main.cpp:11:2: error: 'work' was not declared in this scope
   11 |  work.join();
      |  ^~~~

推荐答案

默认情况下,MinGW-w64带有本机(Win32)而不是POSIX线程支持,而且不幸的是,目前没有Win32 gthreads实现(libstdc ++的线程子系统),因此在GCC中没有线程功能.

MinGW-w64 by default comes with native (Win32) instead of POSIX threads support, and unfortunately there is currently no Win32 gthreads implementation (the threading subsystem of libstdc++), hence no threading functionality in GCC.

您需要从 x86_64-w64-mingw32-g ++-win32 切换到 x86_64-w64-mingw32-g ++-posix 软件包以获得 std ::线程在MinGW-w64中工作.

You need to switch from x86_64-w64-mingw32-g++-win32 to x86_64-w64-mingw32-g++-posix package to get std::thread working in MinGW-w64.

问题 mingw-w64线程:posix vs win32 讨论了此问题更详细.

The question mingw-w64 threads: posix vs win32 discusses this issue in more detail.

这篇关于为什么我的编译器无法识别#include&lt; thread&gt;(c ++)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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