Linux'ta pthread_create için tanımlanmamış referans

Aşağıdaki demoyu internetten https://computing.llnl.gov/tutorials/pthreads/ adresinden aldım

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

Ancak makinemde (Ubuntu Linux 9.04 çalıştıran) derlediğimde aşağıdaki hatayı alıyorum:

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

Bu bana hiç mantıklı gelmiyor, çünkü başlık pthread_create fonksiyonuna sahip olması gereken pthread.h dosyasını içeriyor. Neyin yanlış gittiğine dair bir fikriniz var mı?

Bu soruya şimdiye kadar verilen her iki yanıt da yanlış.
Linux için doğru komut şudur:

gcc -pthread -o term term.c

Genel olarak, kütüphaneler komut satırındaki kaynakları ve nesneleri takip etmelidir ve -lpthread' bir "option" değildir, bir kütüphane spesifikasyonudur. Sadecelibpthread.a` yüklü bir sistemde,

gcc -lpthread ...

bağlantı kurulamayacaktır.

Yorumlar (7)

gcc ile -lpthread seçeneğini kullanmanız gerekir.

Yorumlar (2)

proprieties=>C/C++ build=>GCC C++ Linker=>Libraries=> üst kısmına "Libraries(-l)" "pthread" eklemeniz yeterlidir. işte bu

Yorumlar (0)