Mais
Referência indefinida para pthread_create no Linux
Eu peguei a seguinte demonstração na web em 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);
}
Mas quando o compilo na minha máquina (rodando Ubuntu Linux 9.04), recebo o seguinte erro:
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
Isto não'não faz nenhum sentido para mim, porque o cabeçalho inclui pthread.h
, que deve ter a função pthread_create
. Alguma ideia de que'está a correr mal?
347
3
Ambas as respostas a esta pergunta até agora são incorretas.
Para Linux o comando correto é:
Em geral, bibliotecas devem seguir fontes e objetos na linha de comando, e
-lpthread
não é um "option", it's uma especificação de biblioteca. Em um sistema com apenas o `libpthread.a' instalado,não vai conseguir ligar.
Você precisa utilizar a opção
-lpthread
com gcc.você só precisa adicionar "pthread" em proprieties=>C/C++ build=>GCC C+++ Linker=>Libraries=> top part "Libraries(-l)". é isso