Я продолжаю получать эти ошибки - PullRequest
0 голосов
/ 11 февраля 2020

Я продолжаю получать сообщения об ошибках и не знаю, что они на самом деле означают. /tmp/ccIUS3m4.o: В функции main': assign3_1.c:(.text+0x7f): undefined reference to pthread_create 'assign3_1. c :(. text + 0xa9): неопределенная ссылка на pthread_join' /tmp/ccIUS3m4.o: In function runner': assign3_1. c :(. text + 0x1e6): undefined ссылка на `sqrt 'collect2: ошибка: ld вернул 1 состояние выхода

Ниже мой код:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define NUMBER_OF_POINTS 1000000
#define NUMBER_OF_THREADS 2

void *runner(void *param);
// circle size
int circle_count=0;

// generate a double precision random number
double random_double(){
        return random() / ((double)RAND_MAX + 1);
}

int main (int argc, const char *argv[]){
        int points_per_thread = NUMBER_OF_POINTS/NUMBER_OF_THREADS;
        int i;
        double Pi;
        pthread_t workers[NUMBER_OF_THREADS];

        //create random numbers
        srandom((unsigned)time(NULL));
        clock_t begin = clock();
        for (i=0; i < NUMBER_OF_THREADS; i++)
                pthread_create(&workers[i], 0, runner, &points_per_thread);

        for(i=0; i < NUMBER_OF_THREADS; i++)
                pthread_join(workers[i],NULL);


        //estimating Pi
        Pi = 4.0 * circle_count / NUMBER_OF_POINTS;
        clock_t end = clock();
        double time_spent = (double)(end - begin)/CLOCKS_PER_SEC;
        printf("NUMBER OF POINTS = %d\n", NUMBER_OF_POINTS);
        printf("Pi = %f\n",Pi);
        printf("time = %f\n",time_spent);
        return 0;
        }

void *runner(void *param){
        int POINTS;
        POINTS = *((int *)param);
        int i;
        int hit_count = 0;
        double x, y;

        for(i=0; i < POINTS; i++){
                //generate numbers b/w -1.0 to +1.0
                //to obtain a random (x, y) coordinates
                x = random_double() * 2.0 - 1.0;
                y = random_double() * 2.0 - 1.0;

                //are the points within the circle?
                if (sqrt(x*x + y*y) < 1.0)
                        ++hit_count; //yes
        }

        circle_count += hit_count;

        pthread_exit(0);
}

...