I have a small a program that should start a 2nd Thread. the problem is that, when try to make an executable file of this program using make. I get :
engine@ubuntu:~/Desktop/Lecture$ make thread cc thread.c -o thread thread.c: In function ‘main’: thread.c:10:2: error: unknown type name ‘pthread’ .............here is my code :
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
void *func(void*);
int main(){
pthread tid;
if (pthread_create(&tid,NULL,fun,NULL)!= 0 ){ printf("error by _ pthread \n");
}
printf ( "thread output1 \n");
sleep(1);
return EXIT_SUCCESS; } void *fun(void* data){
printf("thread output2 \n") }I don't think that program is wrong, it maybe the way I'm running the make command ?
any Idea why do I get this. thanks in advance for your help
22 Answers
Did you forget to link a dynamic library?
cc thread.c -o thread -lpthread
Further errors:
1
- type of pthread is pthread_t
- you misspelled fun and func
- no semicolon in function after printf
According to the manual page for pthread_create and some sample code around the internet, the first parameter is a pointer to pthread_t, not pthread. Try that:
pthread_t tid;