相关疑难解决方法(0)

Linux中对pthread_create的未定义引用

我从网上从https://computing.llnl.gov/tutorials/pthreads/上摘下了以下演示

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }
   pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud)

但是当我在我的机器上编译它(运行Ubuntu Linux 9.04)时,我收到以下错误:

corey@ubuntu:~/demo$ gcc -o term term.c …
Run Code Online (Sandbox Code Playgroud)

c linux multithreading pthreads

349
推荐指数
9
解决办法
57万
查看次数

修复 WSL 上的 pthread_create 引用错误

我有一个在多台机器上运行的 C 程序,尽管它在我的 MacBook 上正确编译,但我无法在运行 Ubuntu 18.04 并使用 gcc 的 Linux Windows 子系统上编译该程序。

该程序生成多个线程,当我尝试在 Windows 子系统上编译它时,出现错误undefined reference to `pthread_create

其他几个问题,例如thisthisthis,建议使用-lpthread作为编译器标志来解决问题,但我已经这样做了,并且它在 OSX 上编译得很好,所以我怀疑这个问题可能与我的 WSL 配置有关。

我的生成文件是:

CC = gcc
CCOPTS = -Wall -c -g -ggdb
LINKOPTS = -Wall -g -ggdb -lpthread

all: calc

calc: calc.o smp3_tests.o testrunner.o
    $(CC) $(LINKOPTS) -o $@ $^

calc.o: calc.c calc.h
    $(CC) $(CCOPTS) -o $@ $<

Run Code Online (Sandbox Code Playgroud)

我将 pthread 标头包含在内#include <pthread.h>,并使用 调用该pthread_create函数pthread_create(&multiplierThread, NULL, …

c ubuntu gcc windows-subsystem-for-linux ubuntu-18.04

2
推荐指数
1
解决办法
4303
查看次数