【问题标题】:Getting a collect2: error: ld returned 1 exit status得到一个collect2:错误:ld返回1退出状态
【发布时间】:2020-03-22 13:55:25
【问题描述】:

我正在学习 C 中的二进制搜索,并且我已经运行了这个简单的程序,它将返回我想在该数组中找到的任何数字的数组索引。但是这段代码给了我“collect2:错误:ld 返回 1 退出状态”。我正在将 Clion 与 Cygwin 编译器结合使用。

谁能告诉我我需要在哪里修复代码才能使代码正常工作?而且,将来我不应该做什么来避免这个特定的错误? TIA。

代码如下:

#include <stdio.h>
int searchNumber(int arr[], int n, int x);
//    printf("");

int main() {
    int i, n, store;

    printf("Length of array: ");
    scanf("%d", &n);
    int arr[n];


    //Taking the values of the array
    for(i =0; i<n; i++)
    {
        printf("Enter the value of arr[%d]: ", i);
        scanf("%d", &arr[i]);
    }

    //Printing the values
    for(i =0; i<n; i++)
    {
        printf("Value of arr[%d]: %d\n", i, arr[i]);
    }

    store = searchNumber(arr, n, 5);
    printf("%d", store);

    //custom function
    int searchNumber(int arr[], int n, int x){
        int left, right, mid;
        left =0;
        right = n-1;

        while(left <= right)
        {
            mid = (left + right) / 2;
            if(arr[mid] == x)
            {
                return mid;
            }

            if(x > arr[mid])
                left = mid + 1;

            else{
                right = mid - 1;
            }


        }

return -1;




    }

    return 0;
}

这是完整的错误信息:

   /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles/Binary.dir/BInnarry.c.o: in function `main':
/cygdrive/c/Users/slevin/CLionProjects/mama/BInnarry.c:26: undefined reference to `searchNumber'
/cygdrive/c/Users/slevin/CLionProjects/mama/BInnarry.c:26:(.text+0x12a): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `searchNumber'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/Binary.dir/build.make:84: Binary.exe] Error 1
make[3]: Leaving directory '/cygdrive/c/Users/slevin/CLionProjects/mama/cmake-build-debug'
make[2]: *** [CMakeFiles/Makefile2:134: CMakeFiles/Binary.dir/all] Error 2
make[2]: Leaving directory '/cygdrive/c/Users/slevin/CLionProjects/mama/cmake-build-debug'
make[1]: *** [CMakeFiles/Makefile2:141: CMakeFiles/Binary.dir/rule] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/slevin/CLionProjects/mama/cmake-build-debug'
make: *** [Makefile:144: Binary] Error 2

【问题讨论】:

  • 在此之前还有另一条错误消息,告诉您出了什么问题。您引用的消息只是链接器/加载器说“操作失败 - 没有生成可执行文件”。
  • 另外,您正在使用嵌套函数。那是一个GCC extension,它有自己奇怪的规则。不要那样做。
  • 您显示的错误消息将 apple 引用为缺少的函数 — 您显示的代码中没有对 apple 的引用。您还没有提供外部函数searchNumber(),当您在main() 中调用它时,这是所有可见的。嵌套函数被忽略 - 定义后没有对其的引用。
  • @JonathanLeffler 我已附上完整的错误消息。我能做些什么来解决这个问题?自过去 2 小时以来一直在尝试
  • 取消嵌套函数。

标签: c binary-search


【解决方案1】:

尝试从 main() 函数中移出 searchNumber() 函数定义:

#include <stdio.h>

int searchNumber(int arr[], int n, int x);
//    printf("");

int main() {
    int i, n, store;

    printf("Length of array: ");
    scanf("%d", & n);
    int arr[n];

    //Taking the values of the array
    for (i = 0; i < n; i++) {
        printf("Enter the value of arr[%d]: ", i);
        scanf("%d", & arr[i]);
    }

    //Printing the values
    for (i = 0; i < n; i++) {
        printf("Value of arr[%d]: %d\n", i, arr[i]);
    }

    store = searchNumber(arr, n, 5);
    printf("%d", store);

    return 0;
}

//custom function
int searchNumber(int arr[], int n, int x) {
    int left, right, mid;
    left = 0;
    right = n - 1;

    while (left <= right) {
        mid = (left + right) / 2;
        if (arr[mid] == x) {
            return mid;
        }

        if (x > arr[mid])
            left = mid + 1;

        else {
            right = mid - 1;
        }

    }

    return -1;
}

Online GDB Verification

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多