prachijpp
October 22nd, 2007, 05:19 AM
Here is my code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
#include<pthread.h>
pthread_t hour_thread , min_thread , sec_thread;
time_t rawtime;
struct tm *mytime;
void set_time() {
system("clear");
rawtime = time(&rawtime);
mytime = localtime(&rawtime);
printf("Current time : %s" , asctime(mytime));
}
void * update_hour(void *arg) {
while(1) {
sleep(1);
rawtime = time(&rawtime);
mytime = localtime(&rawtime);
set_time();
}
}
void * update_min(void *arg) {
while(1) {
sleep(1);
rawtime = time(&rawtime);
mytime = localtime(&rawtime);
set_time();
}
}
void * update_sec(void *arg) {
while(1) {
sleep(1);
rawtime = time(&rawtime);
mytime = localtime(&rawtime);
set_time();
}
}
int main() {
int err1 , err2 , err3;
void *value_ptr;
set_time();
if((err1 = pthread_create(&hour_thread , NULL , update_hour , NULL)) != 0)
printf("\n\tError in creating hour thread : %s" , strerror(err1));
if((err2 = pthread_create(&min_thread , NULL , update_min , NULL)) != 0)
printf("\n\tError in creating minute thread : %s" , strerror(err2));
if((err3 = pthread_create(&sec_thread , NULL , update_sec , NULL)) != 0)
printf("\n\tError in creating second thread : %s" , strerror(err3));
pthread_join(hour_thread , &value_ptr);
pthread_join(min_thread , &value_ptr);
pthread_join(sec_thread , &value_ptr);
return(0);
}
When I run it I get following error:
/tmp/ccsOiUW1.o: In function `main':
clock.c:(.text+0x13a): undefined reference to `pthread_create'
clock.c:(.text+0x182): undefined reference to `pthread_create'
clock.c:(.text+0x1ca): undefined reference to `pthread_create'
clock.c:(.text+0x203): undefined reference to `pthread_join'
clock.c:(.text+0x218): undefined reference to `pthread_join'
clock.c:(.text+0x22d): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
Header file pthread.h gets included successfully,
so why it is not detecting function pthread_join, pthread_create
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
#include<pthread.h>
pthread_t hour_thread , min_thread , sec_thread;
time_t rawtime;
struct tm *mytime;
void set_time() {
system("clear");
rawtime = time(&rawtime);
mytime = localtime(&rawtime);
printf("Current time : %s" , asctime(mytime));
}
void * update_hour(void *arg) {
while(1) {
sleep(1);
rawtime = time(&rawtime);
mytime = localtime(&rawtime);
set_time();
}
}
void * update_min(void *arg) {
while(1) {
sleep(1);
rawtime = time(&rawtime);
mytime = localtime(&rawtime);
set_time();
}
}
void * update_sec(void *arg) {
while(1) {
sleep(1);
rawtime = time(&rawtime);
mytime = localtime(&rawtime);
set_time();
}
}
int main() {
int err1 , err2 , err3;
void *value_ptr;
set_time();
if((err1 = pthread_create(&hour_thread , NULL , update_hour , NULL)) != 0)
printf("\n\tError in creating hour thread : %s" , strerror(err1));
if((err2 = pthread_create(&min_thread , NULL , update_min , NULL)) != 0)
printf("\n\tError in creating minute thread : %s" , strerror(err2));
if((err3 = pthread_create(&sec_thread , NULL , update_sec , NULL)) != 0)
printf("\n\tError in creating second thread : %s" , strerror(err3));
pthread_join(hour_thread , &value_ptr);
pthread_join(min_thread , &value_ptr);
pthread_join(sec_thread , &value_ptr);
return(0);
}
When I run it I get following error:
/tmp/ccsOiUW1.o: In function `main':
clock.c:(.text+0x13a): undefined reference to `pthread_create'
clock.c:(.text+0x182): undefined reference to `pthread_create'
clock.c:(.text+0x1ca): undefined reference to `pthread_create'
clock.c:(.text+0x203): undefined reference to `pthread_join'
clock.c:(.text+0x218): undefined reference to `pthread_join'
clock.c:(.text+0x22d): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
Header file pthread.h gets included successfully,
so why it is not detecting function pthread_join, pthread_create