-3

I got error message about "undefined reference to function". How can i solve this? Error message is in the image. We are trying to make a multi-thread sorting program.

First, a series of 800 elements and a random distribution of distributions should be created. The values ​​of the elements are unique. Three scheduled threads will be created. The first thread lists the first 300 elements, while the second thread lists the other 500 threads. After the two workpieces have finished their work, they have not found the third workpiece sequence and the second-row sequences. We have a new set for merging. So it must be joined. In the last step, the listed elements should be taken to the file named "son.txt" which will be created in the working directory.

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define NUM_THREADS 3
    int sayilar[800];
    void *Sirala(void *param);
    void quickSort(int[], int, int);
    int partition(int[], int, int);
    int kontrol=0;

int main(int argc, char *argv[]){
int i,j;
    srand(time(NULL));
    for(i=0; i<800; i++){
        sayilar[i] = 1+ (rand() % 800);
        for(j=0; j<1; j++){
            if(sayilar[j]== sayilar[i]){
                i--;
                break;
                }
        }
    }

    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    for(t=0; t<3; t++){
        rc = threadcreate(&threads[t], NULL, Sirala, (void *)t);
        if(rc){
            printf("Hata, thread oluşturulamadı. Thread: %d\n", rc);
            exit(-1);       
        }
        pthread_join(threads[t], NULL);
    printf("\n\n");
    }
    printf("SON\n");
    for(i=0; i<800; i++)
        printf("%d\n", sayilar[i]);
    printf("/n/n");

    FILE *yaz; //dosya göstericisi
    yaz = fopen("son.txt", "w+");
    if(yaz == NULL){
        puts("Dosya acilmiyor");
        exit(1);
    }
    for(i=0; i<800; i++){
        fprintf(yaz, "%d\n", sayilar[i]);
    }
    fclose(yaz);

    pthread_exit(NULL);

void *Sirala(void *param){
    int i, baslangic, bitis;
    if((long)param ==0){
        baslangic = 0;
        bitis= 300;
        kontrol =1; 
        printf("Dizinin ilk 300 elemanının sırasız hali:\n\n");
        for(i=baslangic; i<bitis; i++){
            printf("%d - ", sayilar[i]);
        }
        printf("/n/n");
    }
    else if((long)param ==1){
        baslangic = 300;
        bitis= 800;
        kontrol =2; 
        printf("Dizinin diğer 500 elemanının sırasız hali:\n\n");
        for(i=baslangic; i<bitis; i++){
            printf("%d - ", sayilar[i]);
        } 
        printf("/n/n");
    }
    else {
        baslangic = 0;
        bitis= 800;
        kontrol =0;
    }
    for(i= baslangic; i<bitis; i++){
        quicksort(sayilar, baslangic, bitis-1);
    }   
    if(kontrol ==1){
    printf("Dizinin 300 elemanının sıralı hali:\n\n");
    for(i = baslangic; i<bitis; i++){
        printf("%d - ", sayilar[i]);
    }   
    }
    else if(kontrol ==2){
    printf("Dizinin diğer 500 elemanının sıralı hali:\n\n");
    for(i = baslangic; i<bitis; i++){
        printf("%d - ", sayilar[i]);
    }   
    }
    printf("\n\n");
    pthread_exit(0);
}

void quickSort(int a[], int sol, int sag){
    int j;
    if(sol<sag){
        j=partition(a,sol, sag);
        quickSort(a, sol, j-1);
        quickSort(a, j+1, sag); 
    }
}

int partition(int a[], int sol, int sag){
    int pivot, i, j, t;
    pivot = a[sol];
    i= sol;
    j= sag+1;
}
}

Error message:

error message

mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • 1
    Typoes (C is case-sensitive), and format your code properly, the other problems should stand out once you do that. – Mat May 23 '18 at 08:12
  • 1
    Please, don't send textual info as image. (Copy/paste it instead.) – Scheff's Cat May 23 '18 at 08:13
  • 1
    This is a linker error, it has nothing to do with the threading code, so please strip your question down to a [minimal example](/help/mcve). Also, include you error message as text (forecast is as code) and **fix your indentation** because wrong bracing is likely the issue. – Frax May 23 '18 at 08:21
  • I cannot read the screen shot. Please post text as text. – alk May 23 '18 at 08:26
  • Check your curly braces. Your functions are inside `main()` – qrdl May 23 '18 at 09:24
  • It looks like you forgot to enable a good set of warnings. For GCC, I recommend `-Wall -Wextra -Wwrite-strings` as a minimum; consider also `-Wpedantic -Warray-bounds` for identifying some other common mistakes. – Toby Speight May 23 '18 at 10:45
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Toby Speight May 23 '18 at 10:45
  • None of what you say has made me solve. I asked for help, not humiliation. – user9389953 May 29 '18 at 14:45

2 Answers2

1

The function you must use to create a new thread is pthread_create not threadcreate.

EDIT: You also forgot to #include<pthread.h> and forgot to close the main function with } at the end. You wrote also quicksort while the funciont is quickSort.

Please reformat your code because it is a mess and it is impossible to understand where functions end.

#include <stdio.h> 
#include <stdlib.h>    
#include <time.h>    
#include<pthread.h>
#define NUM_THREADS 3

int sayilar[800];

void *Sirala(void *param);

void quickSort(int[], int, int);

int partition(int[], int, int);

int kontrol=0;



int main(int argc, char *argv[]){

int i,j;

    srand(time(NULL));

    for(i=0; i<800; i++){

        sayilar[i] = 1+ (rand() % 800);

        for(j=0; j<1; j++){

            if(sayilar[j]== sayilar[i]){

                i--;

                break;

                }

        }

    }



    pthread_t threads[NUM_THREADS];

    int rc;

    long t;

    for(t=0; t<3; t++){

        rc = pthread_create(&threads[t], NULL, Sirala, (void *)t);

        if(rc){

            printf("Hata, thread oluşturulamadı. Thread: %d\n", rc);

            exit(-1);

        }

        pthread_join(threads[t], NULL);

    printf("\n\n");

    }

    printf("SON\n");

    for(i=0; i<800; i++)

        printf("%d\n", sayilar[i]);

    printf("/n/n");



    FILE *yaz; //dosya göstericisi

    yaz = fopen("son.txt", "w+");

    if(yaz == NULL){

        puts("Dosya acilmiyor");

        exit(1);

    }

    for(i=0; i<800; i++){

        fprintf(yaz, "%d\n", sayilar[i]);

    }

    fclose(yaz);



    pthread_exit(NULL);
}


void *Sirala(void *param){

    int i, baslangic, bitis;

    if((long)param ==0){

        baslangic = 0;

        bitis= 300;

        kontrol =1;

        printf("Dizinin ilk 300 elemanının sırasız hali:\n\n");

        for(i=baslangic; i<bitis; i++){

            printf("%d - ", sayilar[i]);

        }

        printf("/n/n");

    }

    else if((long)param ==1){

        baslangic = 300;

        bitis= 800;

        kontrol =2;

        printf("Dizinin diğer 500 elemanının sırasız hali:\n\n");

        for(i=baslangic; i<bitis; i++){

            printf("%d - ", sayilar[i]);

        }

        printf("/n/n");

    }

    else {

        baslangic = 0;

        bitis= 800;

        kontrol =0;

    }

    for(i= baslangic; i<bitis; i++){

        quickSort(sayilar, baslangic, bitis-1);

    }

    if(kontrol ==1){

    printf("Dizinin 300 elemanının sıralı hali:\n\n");

    for(i = baslangic; i<bitis; i++){

        printf("%d - ", sayilar[i]);

    }

    }

    else if(kontrol ==2){

    printf("Dizinin diğer 500 elemanının sıralı hali:\n\n");

    for(i = baslangic; i<bitis; i++){

        printf("%d - ", sayilar[i]);

    }

    }

    printf("\n\n");

    pthread_exit(0);

}



void quickSort(int a[], int sol, int sag){

    int j;

    if(sol<sag){

        j=partition(a,sol, sag);

        quickSort(a, sol, j-1);

        quickSort(a, j+1, sag);

    }

}



int partition(int a[], int sol, int sag){

    int pivot, i, j, t;

    pivot = a[sol];

    i= sol;

    j= sag+1;

}

EDIT 2: You must solve also this warning:

warning: control reaches end of non-void function
  [-Wreturn-type]
}
^
1 warning generated. 
roschach
  • 8,390
  • 14
  • 74
  • 124
1

you should adjust threadcreate to pthread_create, and adjust quicksort to quickSort

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_THREADS 3

int sayilar[800];
void *Sirala(void *param);
void quickSort(int[], int, int);
int partition(int[], int, int);
int kontrol=0;

int main(int argc, char *argv[]){
    int i,j;
    srand(time(NULL));
    for(i=0; i<800; i++){
        sayilar[i] = 1+ (rand() % 800);
        for(j=0; j<1; j++){
            if(sayilar[j]== sayilar[i]){
                i--;
                break;
            }
        }
    }
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    for(t=0; t<3; t++){
        rc = pthread_create(&threads[t], NULL, Sirala, (void *)t);/*here*/
        if(rc){
            printf("Hata, thread oluşturulamadı. Thread: %d\n", rc);
            exit(-1);       
        }
        pthread_join(threads[t], NULL);
        printf("\n\n");
    }
    printf("SON\n");
    for(i=0; i<800; i++){
        printf("%d\n", sayilar[i]);
    }
    printf("/n/n");

    FILE *yaz; //dosya göstericisi
    yaz = fopen("son.txt", "w+");
    if(yaz == NULL){
        puts("Dosya acilmiyor");
        exit(1);
    }

    for(i=0; i<800; i++){
        fprintf(yaz, "%d\n", sayilar[i]);
    }
    fclose(yaz);
    pthread_exit(NULL);
    return 0;
}
void *Sirala(void *param){
    int i, baslangic, bitis;
    if((long)param ==0){
        baslangic = 0;
        bitis= 300;
        kontrol =1; 
        printf("Dizinin ilk 300 elemanının sırasız hali:\n\n");
        for(i=baslangic; i<bitis; i++){
            printf("%d - ", sayilar[i]);
        }
        printf("/n/n");
    }
    else if((long)param ==1){
        baslangic = 300;
        bitis= 800;
        kontrol =2; 
        printf("Dizinin diğer 500 elemanının sırasız hali:\n\n");
        for(i=baslangic; i<bitis; i++){
            printf("%d - ", sayilar[i]);
        } 
        printf("/n/n");
    }
    else{
        baslangic = 0;
        bitis= 800;
        kontrol =0;
    }
    for(i= baslangic; i<bitis; i++){
        quickSort(sayilar, baslangic, bitis-1);/*here*/
    }   
    if(kontrol ==1){
        printf("Dizinin 300 elemanının sıralı hali:\n\n");
        for(i = baslangic; i<bitis; i++){
            printf("%d - ", sayilar[i]);
        }   
    }
    else if(kontrol ==2){
        printf("Dizinin diğer 500 elemanının sıralı hali:\n\n");
        for(i = baslangic; i<bitis; i++){
            printf("%d - ", sayilar[i]);
        }   
    }
    printf("\n\n");
    pthread_exit(0);
}
void quickSort(int a[], int sol, int sag){
    int j;
    if(sol<sag){
        j=partition(a,sol, sag);
        quickSort(a, sol, j-1);
        quickSort(a, j+1, sag); 
    }
}
int partition(int a[], int sol, int sag){
    int pivot, i, j, t;
    pivot = a[sol];
    i= sol;
    j= sag+1;
}
Melody
  • 98
  • 1
  • 4