为 Windows 安装第三方 POSIX 主题

Install third party POSIX Threads for Windows

我找到了一些有关使用 pthreads4w 的详细信息,但找不到完整的示例。安装PThreads4W必须详细做哪些步骤?

以下是我的整理方法。目标是让 pthread 在 Microsoft Windows 上作为系统库可用,这样我就可以 #include <pthread.h> 我的源代码来使用它的功能。

主要问题是 compiler/linker 可以找到库的所有部分,即头文件 pthread.h、可链接库 pthread.lib 及其可执行部分 pthread.dll.它将查看预定义的目录。对于头文件,它将查看环境变量 INCLUDE 或使用环境变量 CL 中的设置。 INCLUDE 仅由构建系统 nmake 使用。使用 Visual Studio 构建会忽略它。 CL 中的设置由编译器使用,因此它适用于两个构建系统,我们将使用它。对于 pthread.lib,编译器查看环境变量 LIB,可执行程序使用 Path 环境变量搜索所需的 pthread.dll

我使用的是 PowerShell。首先,我们必须从 GitHub 下载并安装 pthreads4w。它使用 cmake 并且只能用 nmake 编译所以我们必须使用生成器 'NMake Makefiles'.

PS> git clone https://github.com/jwinarske/pthreads4w.git
PS> cd pthreads4w
PS> cmake -S . -B build -G 'NMake Makefiles' -D BUILD_TESTING=OFF -D CMAKE_BUILD_TYPE=Debug
PS> cmake --build build --config Debug --target install
    --- snip ---
Install the project...
-- Install configuration: "Debug"
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/bin/pthreadVCE3d.dll
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/lib/pthreadVCE3d.lib
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/bin/pthreadVSE3d.dll
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/lib/pthreadVSE3d.lib
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/bin/pthreadVC3d.dll
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/lib/pthreadVC3d.lib
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/lib/libpthreadVCE3d.lib
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/lib/libpthreadVSE3d.lib
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/lib/libpthreadVC3d.lib
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/include/_ptw32.h
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/include/pthread.h
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/include/sched.h
-- Installing: C:/Users/ingo/devel/pthreads4w/PTHREADS-BUILT/x86_64/Debug/include/semaphore.h

这是您找到所有头文件、库和 dll 的地方。或者更清楚的东西:

.\PTHREADS-BUILT\
└── x86_64
    └── Debug
        ├── bin
        │   ├── pthreadVC3d.dll
        │   ├── pthreadVCE3d.dll
        │   └── pthreadVSE3d.dll
        ├── include
        │   ├── _ptw32.h
        │   ├── pthread.h
        │   ├── sched.h
        │   └── semaphore.h
        └── lib
            ├── libpthreadVC3d.lib
            ├── libpthreadVCE3d.lib
            ├── libpthreadVSE3d.lib
            ├── pthreadVC3d.lib
            ├── pthreadVCE3d.lib
            └── pthreadVSE3d.lib

还有一些库使用例外,但我们只使用默认和推荐的共享版本 pthreadVC3d。有关此命名的详细信息,请查看项目的自述文件。

现在我们为通​​用库名称 pthread.lib 提供符号链接并设置环境变量。

PS> cd PTHREADS-BUILT\x86_64\Debug\lib\
PS> new-item -itemtype symboliclink -path . -name pthread.lib -value pthreadVC3d.lib
PS> $env:CL = "/I C:\Users\ingo\devel\pthreads4w\PTHREADS-BUILT\x86_64\Debug\include"
PS> $env:LIB += ";C:\Users\ingo\devel\pthreads4w\PTHREADS-BUILT\x86_64\Debug\lib"
PS> $env:Path += ";C:\Users\ingo\devel\pthreads4w\PTHREADS-BUILT\x86_64\Debug\bin"

不要忘记加号和起始分号。为了测试它,我使用这个简单的程序:

PS> type .\simple_pthread.cpp

#include <pthread.h>

#include <iostream>

namespace // no name, i.e. anonymous for file scope
          // this is the C++ way for decorator STATIC
{
void* start_routine(void*) {
    std::cout << "-- Hello from POSIX Thread" << std::endl;
    return 0; // calls pthread_exit()
}
} // namespace

int main() {
    pthread_t thread;
    int rc;
    void* retval;

    std::cout << "-- starting POSIX Thread" << std::endl;

    rc = pthread_create(&thread, NULL, &start_routine, NULL);
    if (rc != 0) {
        std::cerr << "Error! unable to create thread, rc=" << rc << std::endl;
        exit(1);
    }

    rc = pthread_join(thread, &retval);
    if (rc != 0) {
        std::cerr << "Error! Unable to join thread with rc=" << rc << std::endl;
        exit(1);
    }
    if (retval != NULL) {
        std::cerr << "Error! Thread failed with retval=" << retval << std::endl;
        exit(1);
    }

    return 0;
}

现在使用它(无论你把它存储在哪里):

PS> cl /EHsc .\simple_pthread.cpp pthread.lib
PS> .\simple_pthread.exe
-- starting POSIX Thread
-- Hello from POSIX Thread