slevin
slevin

Reputation: 35

Getting a collect2: error: ld returned 1 exit status

I am learning Binary Search in C and I have run this simple program that will return me the index of an array for any number I want to find in that array. But this code is giving me "collect2: error: ld returned 1 exit status." I am using Clion combined with Cygwin compiler.

Can anybody tell me where I need to fix the code in order to make the code work? And, what should I not do in the future to avoid this particular error? TIA.

Here is the code:

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

Here is the full error message:

   /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

Upvotes: 1

Views: 1856

Answers (1)

rangerz
rangerz

Reputation: 605

Try to move out the searchNumber() function define from main() function:

#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

Upvotes: 1

Related Questions