0

Here is the source code for my qsort.c file. In main, I simply call my_qsort and print the resulting array.

#include "qsort.h"


int partition(int* arr, int left, int right)
{
    int pivot = arr[right], i = left - 1, temp = 0;

    for (int j = 0; j < right; j++)
    {
        if (arr[j] < pivot)
        {
            i++;
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
    temp = arr[i + 1];
    arr[i + 1] = arr[right];
    arr[right] = temp;
    return i + 1;
}

void my_qsort_recursize(int *arr, int left, int right)
{
    int partition_index = 0;
    if (left > right)
    {
        partition_index = partition(arr, left, right);

        my_qsort_recursive(arr, left, partition_index - 1);
        my_qsort_recursive(arr, partition_index + 1, right);
    }
}


void my_qsort(int *arr, int size)
{
    my_qsort_recursive(arr, 0, size - 1);
}

when I run the command "gcc main.c qsort.c" I get the error:

/tmp/ccMikxiR.o: In function `my_qsort_recursize':
qsort.c:(.text+0x18b): undefined reference to `my_qsort_recursive'
qsort.c:(.text+0x1a2): undefined reference to `my_qsort_recursive'
/tmp/ccMikxiR.o: In function `my_qsort':
qsort.c:(.text+0x1cb): undefined reference to `my_qsort_recursive'
collect2: error: ld returned 1 exit status

I don't understand why it is saying this since I am specifying both main.c and qsort.c in the gcc commmand. Am I making a very silly mistake here? I have written many functions like this before and I have never had this problem before.

zadgor
  • 1

1 Answers1

1

you mispelled my_qsort_recursive with my_qsort_recursize in the function declaration.

What kind of text editor/ide are you using? Usually they'll catch this mistake. I recommend visual studio for c/c++ :)

flyingCode
  • 132
  • 2
  • 10
  • 1
    "I recommend visual studio for c/c++" --> Note that visual studio for C, as of 2019, compliance with C99, C11, C17/18 is [incomplete](https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019). Also [Can Visual Studio 2019 Community edition enforce strict ANSI C compliance?](https://stackoverflow.com/a/57471997/2410359). – chux - Reinstate Monica Feb 13 '20 at 04:37