【问题标题】:collect2: error: ld returned 1 exit status, undefined referencecollect2:错误:ld 返回 1 退出状态,未定义的引用
【发布时间】:2021-06-20 20:28:09
【问题描述】:

对这里的编程比较陌生。我在这里很难弄清楚如何让这段代码编译,但不断收到这个错误:

我不一定认为它与函数 insert_array_ascend、get_value、read_value 或 is_in_array 有任何关系,但公平地说,你永远不知道。

/tmp/ccB0YyFX.o: In function `main':
lab.c:(.text+0xb7): undefined reference to `print_array'
collect2: error: ld returned 1 exit status
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void insert_array_ascend(int array[], int n_count);
void print_array(int array[], int size);
int get_value(char *prompt);
int read_file(int array[], char *fname);
int is_in_array(int array[], int size, int input);



int main(){

    int primary[100];
    int count = 0;
    int i;
    char choice;

    count += read_file(primary, "input_numbers.txt");
    do{
            int user_value = get_value("Enter a value to test: ");
            int result = is_in_array(primary, count, user_value);
            primary[count] = user_value;

            insert_array_ascend(primary, count);
            count++;

            print_array(primary, count);
            if (result == 1){
                    printf("TRUE\n");
            }
            else{
                    printf("FALSE\n");
            }
            printf("Would you like to run again? [y/n]: ");
            scanf("%c", &choice);
    } while (choice == 'y' || choice == 'Y');
    return EXIT_SUCCESS;
}

int read_file(int array[], char *fname){

    int position = 0;

    FILE *fp = fopen(fname, "r");
    while (fscanf(fp, "%d", &array[position]) != EOF){
            insert_array_ascend(array, position);
            position++;
    }
return position;
}

void insert_array_ascend(int array[], int size){

    int i, key, j;
    for (i = 1; i <= size; i++){

            j = i;
            while (j > 0 && array[j -1] > array[j]){
                    key = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = key;
                    j--;
                    }
            }
    }

int is_in_array(int array[], int size, int input){

    int l = 0;
    int h = size - 1;
    while (l <= h){

            int checker = (l + h) / 2;
            if (array[checker] == input){
                    return 1;
                    }
            else if (input < array[checker]){
                    h = checker - 1;
                    }
            else{
                    l = checker + 1;
                    }
            }
    }


int get_value(char *prompt){

    int amount;

    printf("%s", prompt);
    scanf("%d", &amount);

    printf("\n Echo printing, read in the number = %d\n\n", amount);

    return amount;
}

我应该怎么做才能使它正确运行?有点菜鸟,所以请帮助外行术语。非常感谢!

【问题讨论】:

    标签: arrays c compiler-errors


    【解决方案1】:

    你没有写函数print_array所以编译器找不到它。

    你需要写它。

    void print_array(int array[], int size);
    

    只通知编译器这个函数在代码中的某个地方。

    【讨论】:

      【解决方案2】:

      错误的意思是“函数main调用了一个名为print_array的函数,但在任何地方都找不到print_array的代码”。

      您还有其他包含print_array 的实现/代码的.c 文件吗?因为在您发布的代码中没有任何这样的实现。

      如果你有一个print_array 的实现,它会是这样的:

      void print_array(int array[], int size) {
          // The implementation/code of print_array goes here.
      }
      

      要解决它,要么在 main 中删除 print_array(primary, count);,或者,如果你需要它,然后像我上面写的那样添加实现。然后错误就会消失。

      【讨论】:

        猜你喜欢
        • 2020-10-04
        • 1970-01-01
        • 1970-01-01
        • 2012-08-02
        • 2021-08-04
        • 2013-06-01
        • 2014-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多