【问题标题】:CMake : <pthread.h> not found in WindowsCMake:在 Windows 中找不到 <pthread.h>
【发布时间】:2012-10-24 11:51:42
【问题描述】:

我是 CMake 的新手。我在 Linux 上针对我正在制作的程序对其进行了测试。该程序使用(POSIX Threads lib),所以在我的 CMakeList 中,我添加了:

find_package(Threads)

它适用于 Linux 发行版(Arch、Mint、Ubuntu,...),但现在,我正在 Windows32(Visual Studio 9 2008)中尝试它,并且在生成过程中收到此消息:

-- Looking for include file pthread.h - not found

(当我编译输出项目文件时,确实没有找到pthread.h)。

在 Windows 上,考虑到“C:\pthread”作为我的 pthread 目录,我在路径中定义:

  • “C:\pthread\include”(著名的“pthread.h”所在的位置)
  • “C:\pthread\”(在 CMake 在某处寻找“包含”的情况下)

但我仍然得到同样的错误(即使在删除缓存之后)。我知道我可以在我的项目中“手动”添加 Pthread,或者在 CMakeList.txt 中定义一些常量,但我认为这不是 CMake 的原则:我可以在所有系统上使用相同的“CMakeList.txt”,对吗?那么我怎么能告诉 CMake “嘿!看起来在这里!Pthread 在这个目录中!”。 可能Cmake没有在PATH中查找,而是在另一个环境变量中查找,但是我没有找到这个信息。

感谢您的阅读。

编辑:我不知道它是否有所作为,但我的项目是 C++ 项目(不是 C)

【问题讨论】:

  • 由于 Windows 上没有包含和库文件的标准路径,因此在运行 cmake-gui 时,CMake 经常需要长时间的帮助。第二种选择是许多查找器查看环境变量。因此,例如要在我的 Windows 安装中找到 ITK,我在启动 cmake-gui 的 cmd.exe 会话中设置了 ITK_DIR。很多包裹也是如此。还有一些 find_package 选项,您可以使用这些选项来帮助控制 CMake 在哪里查找可以提供帮助的包。
  • 查看 CMake 2.8.10 中 FindThreads 的代码,在 windows 上它看起来会默认使用 windows 线程。
  • 我没有测试过。查看是否设置了 CMAKE_USE_PTHREADS_INIT 或仅设置了 CMAKE_USE_WIN32_THREADS_INIT。

标签: windows header include pthreads cmake


【解决方案1】:

我做了什么,我编辑了cmake文件:

option(CMAKE_USE_WIN32_THREADS_INIT "using WIN32 threads" ON)

option(gtest_disable_pthreads "Disable uses of pthreads in gtest." ON)

(我正在使用谷歌测试)

【讨论】:

    【解决方案2】:

    据我所知,Windows 平台本身不支持 Pthreads。除非你使用类似的东西

    win services for unix

    Windows 只有 win32 线程。

    但是,这是一个在 windows 上提供 pthreads 的项目

    pthreads on win32

    【讨论】:

    • err 好吧,我以为你在谈论你在这里复制的 linux pthread 包。
    • 不,我得到了用于 win32 的 pthread,带有头文件和预编译的包。但是 CMake 没有找到它
    【解决方案3】:

    显然 CMAKE_USE_WIN32_THREADS_INIT 在所有平台的上下文中都很有用。该变量在调用 findPackage(Threads) 时生成或初始化,理想情况下,它处理所有平台上的链接问题,以防线程库需要与可执行文件链接。基本上,它在平台 unix 等平台上生成适当的线程库名称,并且在不需要显式线程库进行链接的窗口等平台上为空。 参考:CMake findThreads https://cmake.org/cmake/help/v3.0/module/FindThreads.html?highlight=threads

    【讨论】:

    • 我已经查看了链接,但现在怎么办?你能一步一步详细解释一下吗?
    猜你喜欢
    • 1970-01-01
    • 2017-09-25
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    相关资源
    最近更新 更多