Possible Duplicate:
undefined reference to pthread_create in linux (c programming)
There is the following program:
void *thread(void *vargp);
int main() {
pthread_t tid;
pthread_create(&tid, NULL, thread, NULL);
exit(0);
}
/* thread routine */
void *thread(void *vargp) {
sleep(1);
printf("Hello, world!\n");
return NULL;
}
I am supposed to correct it. I allready added the includes left:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
But I still get the following errors:
/tmp/ccHwCS8c.o: In function `main':
1.c:(.text+0x29): undefined reference to `pthread_create'
collect2: ld returned output state 1
I tried adding -lpthread with the compiler like the answers sayd but I still get this error codes:
@lap:~$ gcc -Wall -lpthread 1.c -o uno
/tmp/ccl19SMr.o: In function `main':
1.c:(.text+0x29): undefined reference to `pthread_create'
collect2: ld returned exit state 1
You need to compile it with
-lpthreadflag in order to link thelibpthreadto your executable.Also you should to add the pthread_join() function in order to let your main thread to wait for the new thread ending. In your current code you will not see the
Hello Worldbecause main thread ending will cause all son threads exiting.