c-linux-debian 未定义对多线程程序中函数的引用 [已关闭]

c-linux-debian undefined reference to function in multi thread program [closed]

提问人:user9389953 提问时间:5/23/2018 最后编辑:mouvicieluser9389953 更新时间:5/23/2018 访问量:154

问:


这个问题是由错别字或无法再现的问题引起的。虽然类似的问题可能在这里是主题,但这个问题的解决方式不太可能帮助未来的读者。

5年前关闭。

我收到有关“未定义函数引用”的错误消息。我该如何解决这个问题?错误消息在图像中。 我们正在尝试制作一个多线程排序程序。

首先,应创建一系列 800 个元素和分布的随机分布。元素的值是唯一的。将创建三个计划线程。第一个线程列出前 300 个元素,而第二个线程列出其他 500 个线程。两个工件完成工作后,仍未找到第三工件序列和第二排工序。我们有一个新的合并集。所以它必须被加入。在最后一步中,列出的元素应被带到名为“son.txt”的文件中,该文件将在工作目录中创建。

    #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

c linux debian 链接器错误 定义引用

评论

1赞 Mat 5/23/2018
拼写错误(C 区分大小写),并正确格式化代码,一旦你这样做了,其他问题应该会突出。
1赞 Scheff's Cat 5/23/2018
请不要以图像形式发送文本信息。(请改为复制/粘贴。
1赞 Frax 5/23/2018
这是一个链接器错误,它与线程代码无关,因此请将您的问题简化为一个最小的示例。此外,将错误消息作为文本包含(预测为代码)并修复缩进,因为错误的支撑可能是问题所在。
0赞 alk 5/23/2018
我无法阅读屏幕截图。请以文本形式发布文本。
0赞 qrdl 5/23/2018
检查您的卷曲牙套。您的功能在里面main()

答:

1赞 roschach 5/23/2018 #1

创建新线程时必须使用的函数不是 。pthread_createthreadcreate

编辑:您还忘记并忘记在最后关闭主要功能。你也写了,而功能是.#include<pthread.h>}quicksortquickSort

请重新格式化你的代码,因为它是一团糟,不可能理解函数在哪里结束。

#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;

}

编辑2:您还必须解决此警告:

warning: control reaches end of non-void function
  [-Wreturn-type]
}
^
1 warning generated. 
1赞 Melody 5/23/2018 #2

您应该将 threadcreate 调整为 pthread_create,并将 quicksort 调整为 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;
}

评论

0赞 Melody 5/23/2018
顺便说一句,在原始代码中,您会在 pthread_exit(NULL) 之后错过一个“}”;