【问题标题】:How to run Pthreads on windows如何在 Windows 上运行 Pthreads
【发布时间】:2011-09-24 22:20:19
【问题描述】:

我以前用mac写过一些C程序,现在不行了。
我必须使用旧的 Windows 笔记本电脑一段时间。

我安装了代码块并使用 Pthreads 测试了一个简单的程序。不幸的是,它没有用。

  pthread_create(&thrd1, NULL, thread_execute, (void *)t);

它一直说对_imp__pthread_create的未定义引用

我该如何解决?

【问题讨论】:

  • 如果你使用 MinGW this 回答可能很有价值。
  • 如果你使用 MinGw this 回答可能很有价值。

标签: c compiler-construction pthreads-win32


【解决方案1】:

您显然已经获得了适用于 Windows 的 pthreads 版本。您只是没有在链接器设置中包含 .lib 文件。这样做,你应该是金子。

【讨论】:

  • 我已经在这里安装了这个 pthreads-w32-2-8-0-release.exe... 我怎样才能将它包含在我的链接器中?抱歉我不习惯在windows上写代码
  • 我觉得你需要链接-lpthreadGC2或者-lpthreadGC
  • 我在哪里可以在 CodeBlocks 上做到这一点?
  • 我不知道关于 CodeBlocks 的第一件事。我猜想 GUI 中有可用的链接器设置。
【解决方案2】:

您需要获取pthreads-win32,因为 pthreads 是 Unix 组件而不是 Windows 组件。

【讨论】:

    【解决方案3】:

    如果您使用 MinGW,您可以使用 MinGW 安装管理器并安装需要执行 pthread 和 openmp 相关任务的包。这是程序。

    打开安装管理器后转到所有包,选择使用mingw32-pthreads-w32命名的选择包并选择它们进行安装。

    然后转到安装 -> 应用更改以安装新软件包。您可以在您的 c 或 c++ 程序中使用 pthread.h 和 omp.h 没有任何问题。

    【讨论】:

      【解决方案4】:

      此代码在 Windows 上的 MSYS2 终端中运行良好。
      您需要做的就是安装gcc。 (见下文。)

      //  hello.c
      #include <omp.h>
      #include <pthread.h>
      #include <stdio.h>
      #include <stdlib.h>
      
      void *print_hello(void *thrd_nr) {
        printf("Hello World. - It's me, thread #%ld\n", (long)thrd_nr);
        pthread_exit(NULL);
      }
      
      int main(int argc, char *argv[]) {
        printf(" Hello C code!\n");
        const int NR_THRDS = omp_get_max_threads();
        pthread_t threads[NR_THRDS];
        for(int t=0;t<NR_THRDS;t++) {
          printf("In main: creating thread %d\n", t);
          pthread_create(&threads[t], NULL, print_hello, (void *)(long)t);
        }
        for(int t=0;t<NR_THRDS;t++) {
          pthread_join(threads[t], NULL);
        }
        printf("After join: I am always last. Byebye!\n");
        return EXIT_SUCCESS;
      }
      

      编译运行如下:

      gcc -fopenmp -pthread hello.c && ./a.out # Linux
      gcc -fopenmp -pthread hello.c && ./a.exe # MSYS2, Windows
      

      如您所见,Linux 和 MSYS2 在 Windows 上的唯一区别 是可执行二进制文件的名称。其他一切都是一样的。 我倾向于认为 MSYS2 是一个模拟的 (Arch-)Linux 终端 窗户。

      在 MSYS2 中安装gcc

      yes | pacman -Syu gcc
      

      期望输出类似于:

       Hello C code!
      In main: creating thread 0
      Hello World. - It's me, thread #0
      In main: creating thread 1
      Hello World. - It's me, thread #1
      After join: I am always last. Bye-bye!
      

      参考

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-01
        • 2018-07-07
        • 2015-11-29
        • 2011-09-22
        • 2013-09-17
        • 2014-07-25
        • 1970-01-01
        相关资源
        最近更新 更多