Hi. I'm having compile errors with the following code which I cannot debug:
Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NTHREADS 32
void *Hello(void *threadid){
int i;
double result = 0.0;
sleep(3);
for(i = 0; i < 1000; i++){
double di = (double) i;
result += sin(di) * tan(di);
}
printf("From thread %d:, Hello world!\n", (int) threadid);
pthread_exit(NULL);
}
int main(){
pthread_t threads[NTHREADS];
int rc;
int t;
for(t = 0; t < NTHREADS; t++){
rc = pthread_create(&threads[t], NULL, Hello, (void *) t);
if(rc){
printf("OMG BEARZZZZ with return code %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
The errors:
Code:
/tmp/ccaU3MYJ.o: In function `Hello':
threads-math.c:(.text+0x2d): undefined reference to `sin'
threads-math.c:(.text+0x3b): undefined reference to `tan'
/tmp/ccaU3MYJ.o: In function `main':
threads-math.c:(.text+0xc3): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
I even try compiling with the following options but still to no avail:
Code:
gcc -L /usr/include/pthread.h /usr/include/math.h -o threads-math threads-math.c
I am particularly frustrated by math.h . The following code compiles well:
Code:
#include <stdio.h>
#include <math.h>
main(){
double test = sin(42.0);
printf("sin(42) = %f\n", test);
}
I am compiling under Ubuntu 9.10 with gcc version: gcc (Ubuntu 4.4.1-4ubuntu9) 4.4.1. Any ideas why the "undefined reference"s?