Cuando traté de compilar este programa, que fracasaron:¿Por qué obtengo errores de "referencia indefinida" incluso cuando incluyo los archivos de encabezado correctos?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *WriteNumbers(void *threadArg)
{
int start, stop;
start = atoi((char *)threadArg);
stop = start + 10;
while (start < stop)
{
printf("%d\n", start++);
sleep(1);
}
return 0;
}
int main(int argc, char **argv)
{
pthread_t thread1, thread2;
// create the threads and start the printing
pthread_create(&thread1, NULL, WriteNumbers, (void *)argv[1]);
pthread_create(&thread2, NULL, WriteNumbers, (void *)argv[2]);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
Me dio los siguientes errores:
tmp/ccrW21s7.o: In function `main':
pthread.c:(.text+0x83): undefined reference to `pthread_create'
pthread.c:(.text+0xaa): undefined reference to `pthread_create'
pthread.c:(.text+0xbd): undefined reference to `pthread_join'
pthread.c:(.text+0xd0): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
¿Por qué me dan estos errores de referencia a pesar de que yo había incluido pthread.h
, que declara estas funciones?
También tiene otros problemas: no está comprobando el valor de 'argc' pero está utilizando los valores de' argv'. Su método 'WriteNumbers' no devuelve un valor. – dreamlax
@dreamlax: he solucionado el problema de "no devolución"; No noté el problema argc/argv. –