在Linux中对pthread_create的未定义引用

我从https://computing.llnl.gov/tutorials/pthreads/在网络上获取了如下演示 ubuntu

#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);
}

可是,当我在运行Ubuntu Linux 9.04的计算机上对其进行编译时,出现如下错误: 网络

corey@ubuntu:~/demo$ gcc -o term term.c
term.c: In function ‘main’:
term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
/tmp/cc8BMzwx.o: In function `main':
term.c:(.text+0x82): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

这对我来讲没有任何意义,由于标头包含pthread.h ,应具备pthread_create函数。 任何想法出什么事了吗? 函数


#1楼

您只须要在proprieties => C / C ++ build => GCC C ++ Linker => Libraries =>顶部“ Libraries(-l)”中添加“ pthread”。 而已 ui


#2楼

日食 spa

属性-> c / c ++ Build->设置-> GCC C ++连接器->顶部的库中添加“ pthread” 命令行


#3楼

在Anjuta中,转到“生成”菜单,而后“配置项目”。 在“配置选项”框中,添加: code

LDFLAGS='-lpthread'

但愿它也能帮助别人... 对象


#4楼

到目前为止,这个问题的两个答案都是错误的
对于Linux,正确的命令是: ci

gcc -pthread -o term term.c

一般,库应该在命令行中跟随源和对象,而且-lpthread不是“选项”,它是库规范。 在仅安装libpthread.a系统上, get

gcc -lpthread ...

将没法连接。


#5楼

有时,若是您使用多个库,请检查库依赖性。 (例如,-lpthread -lSDL ... <==> ... -lSDL -lpthread)

相关文章
相关标签/搜索