CMake:在 Windows 中找不到 <pthread.h>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13218646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:02:06  来源:igfitidea点击:

CMake : <pthread.h> not found in Windows

windowsheaderincludepthreadscmake

提问by Neozaru

I'm newbie with CMake. I tested it on Linux for a program I am making. This program uses (POSIX Threads lib), so in my CMakeList, I added :

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

find_package(Threads)

It works on Linux distribs (Arch, Mint, Ubuntu, ...), but now, I'm trying it in Windows32 (Visual Studio 9 2008), and I get this message during generation :

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

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

(and when I compile output project file, pthread.h is indeed not found).

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

On Windows, considering "C:\pthread" as my pthread directory, I defined in path :

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

  • "C:\pthread\include" (where resides the famous "pthread.h")
  • "C:\pthread\" (in the case where CMake looks for an "include" somewhere)
  • “C:\pthread\include”(著名的“pthread.h”所在的位置)
  • "C:\pthread\"(在 CMake 在某处寻找“包含”的情况下)

But I still get the same error (even after deleted cache). I know I could "manually" add Pthread in my Project, or define some constants in CMakeList.txt, but I thinks it's not the principle of CMake : I could use the SAME "CMakeList.txt" on all systems, right ? So how can I tell to CMake "Hey ! Looks here ! Pthread is in this directory !". Maybe Cmake doesn't look in PATH, but in another environment variable, but I didn't find this information.

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

Thank you for reading.

感谢您的阅读。

EDIT : I don't know if it makes a difference, but my project is a C++ project (not C)

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

回答by radato

What I did, I edited the cmake file:

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

option(CMAKE_USE_WIN32_THREADS_INIT "using WIN32 threads" ON)

and

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

(I am using google test)

(我正在使用谷歌测试)

回答by fkl

As far as i know, Pthreads is not natively supported on windows platform. Unless you use some thing like

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

win services for unix

为 unix 赢得服务

Windows only has win32 threads.

Windows 只有 win32 线程。

However, this is a project which provides pthreads on windows

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

pthreads on win32

win32 上的线程

回答by Saif Mulla

Apparently the CMAKE_USE_WIN32_THREADS_INITis useful in context of all platforms. This variable is generated or initialised on invocation of findPackage(Threads), ideally it handles linking issues on all platforms in case thread library is required to be linked with executable. Basically it generates appropriate thread library name on platform unix like platforms and is empty on platform such as windows where explicit threading library is not required for linking. Reference: CMake findThreads https://cmake.org/cmake/help/v3.0/module/FindThreads.html?highlight=threads

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