I was doing the sorting part and was having trouble in figuring out what file to compile as to check if my code is correct for bubble sort or not. So started searching here and someone posted here stating that to create a separate file and initialize an array of lesser elements and then link it with helpers.c with the terminal command

clang sort.c helpers.c -o sort

where sort.c is name of my separate program file. It worked and my code for bubble sort turned out to be correct. Then just for practicing a bit more before going further into the Pset I thought of making sort.c take a command line argument to determine the size of array then take in the elements of the array through GetInt() This is the code that i have written for this

#include <cs50.h>
#include <stdio.h>
#include "helpers.h"
#include <stdlib.h>

int main (int argc, string argv[])
    int m = atoi(argv[1]);
    int values[m] ;
    for (int i = 0; i < m; i++)
        values[i] = GetInt();

    sort (values,m);

    for (int k = 0; k < m; k++)
        printf("no %i : %i\n", k , values[k]);


now when I try to compile this code with : clang sort.c helpers.c -o sort I get this error

/tmp/sort-880e5b.o: In function `main':
sort.c:(.text+0x5b): undefined reference to `GetInt'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

any idea why am i getting this and how to fix it?


You must tell the compiler to link the cs50 library by using the -lcs50 flag. The full command should be:

clang -ggdb3 -O0 -std=c99 -Wall -Werror sort.c helpers.c -lcs50 -o sort

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .