【问题标题】:Visual Studio 2017, How to make #include <pthread.h> work?Visual Studio 2017,如何使#include <pthread.h> 工作?
【发布时间】:2021-08-11 19:54:36
【问题描述】:

我觉得我今天一直在寻找如何让 #include &lt;pthread.h&gt; 在 Windows 10 上的 Visual Studio 2017 中工作的几个小时,但没有找到有效的答案。有人可以指导我如何让它工作吗?

我收到的错误是:无法打开源文件“pthread.h”

【问题讨论】:

  • libpthread 通常用于 *nix。找到 Windows 版本了吗?
  • pthread 用于 Linux POSIX 线程,不与 Visual Studio 捆绑在一起。如果您想在 Windows 上创建线程,请查找 msdn.microsoft.com/en-us/library/windows/desktop/… 或使用 OpenMP
  • 如果你想避免这个问题,你应该可以使用 Linux 虚拟机。

标签: c visual-studio-2017


【解决方案1】:

在 Visual Studio 2017 中

  1. 项目 -> 管理 Nuget 包

  2. 在浏览选项卡中搜索“pthread”

  3. 选择安装[在 Nuget 包中搜索结果]

【讨论】:

  • 这是最快的方法
  • 我收到一个错误 "1>c:\users\timo\source\repos\consoleapplication1\packages\pthreads.2.9.1.4\build\native\include\pthread.h(320): 错误C2011: 'timespec': 'struct' 类型重定义 1>c:\program files (x86)\windows kits\10\include\10.0.17763.0\ucrt\time.h(39): 注意:见 'timespec' 的声明"
  • @TraceKira 我遇到了同样的错误。 pthread.h 中似乎有一个错误。如果您在该规范中发表评论,一切都会再次正常。
  • @TraceKira 在 pthread.h 文件中,删除 318-324 之间的行。它会解决重定义问题
【解决方案2】:

对于 Visual Studio 2017,我已通过 NuGet 包进行安装。

试试下面的步骤,

  1. 转到项目>“管理 NuGet 包”
  2. 浏览 > 搜索“pthread”> 安装

【讨论】:

    【解决方案3】:

    Windows 本身不支持 pthread。

    您可以使用an implementation of pthreads for Windows。如果您只是想尽快让一些代码在 Windows 上运行,那可能是您最好的选择。

    如果您可以修改代码,您可能想看看您实际使用了哪些 pthread 函数;如果您只需要一个互斥体,那么 pthreads/winapi 周围的几个#ifdefs 可能是更好的选择。

    还有一些项目实现的兼容性层不如完整的 pthreads API 富有表现力,但对于大多数项目来说已经绰绰有余了。 TinyCThread 是一个实现 C11 线程 API 的选项(我坚持,所以建议稍加考虑)。

    其他选项包括:

    OpenMP 也是一个不错的选择。它真的很容易使用,但在更高级别上运行,因此可能需要更大量的重写。

    【讨论】:

    【解决方案4】:

    如果你安装了 vcpkg,那么你可以这样做:

    vcpkg.exe install pthread
    

    这将从https://sourceforge.net/projects/pthreads4w 下载一些代码,编译并安装到您的系统中。如果 vcpkg 在您的机器上正确设置,Visual Studio 应该会自动使用这些文件。

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 2017-08-02
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      相关资源
      最近更新 更多