#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void* thread_main(void *argument){
int i;
int cnt = *((int*)argument);
for(i = 0; i < cnt; i++){
sleep(1);
puts("working....");
}
return NULL;
}
int main(){
pthread_t t_id;
int thread_param = 5;
if(pthread_creat(&t_id, NULL, thread_main, (void*)&thread_param) != 0){
puts("pthread_creat() error ");
return -1;
};
sleep(10);
puts("main end");
return 0;
}
>> gcc -o thread thread.c -lpthread
thread.c: In function ‘main’:
thread.c:23:5: warning: implicit declaration of function ‘pthread_creat’ [-Wimplicit-function-declaration]
if(pthread_creat(&t_id, NULL, thread_main, (void*)&thread_param) != 0){
^
/tmp/ccERlxNy.o: In function `main':
thread.c:(.text+0x81): undefined reference to `pthread_creat'
collect2: error: ld returned 1 exit status
It's test for basic working thread. but I can't compile this. what's the problem?
OS is linux ubuntu 64bit
What I have tried:
i try to link use gcc -l option, but it was not working.
thread.c:(.text+0x81): undefined reference to `pthread_creat' in google search, every answer are same, just add -lpthread . what the problem
header files
#include <stdio.h>
#include <unistd.h>
#include <pthread.h></pthread.h></unistd.h></stdio.h>