2

I've made a program to test my sort function. The only problem is that when I try to compile it gives me the "undefined reference to 'sort'" error as seen in the below:

Error message in terminal

For context, I have taken the following steps already:

  • Run update50, closed, and reopened gedit/terminal
  • Checked that find.c does not produce the same error for sort or search,and uses the exact same #include syntax as the file testsort.c
  • Confirmed that when I call the sort or search function, they both produce the same error.

2 Answers 2

5

this is an example on a program with multiple source code files. C allows you to call a function that is defined in one file from another. we do that all the time (e.g., when we call printf, obviously it's not defined in our source code file).

to make use of that, all you need to do is to compile your source code files and link the object files together to produce the final executable. see https://www.youtube.com/watch?v=ED7QtgXDShY and https://www.youtube.com/watch?v=CSZLNYF4Klo!

this is done for you in find. if you look at the makefile, you'll find that if you execute make find, this command gets executed

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

as you can probably see, the command includes both find.c and helpers.c.

this instructs clang to compile these files and link the produced object files together to produce the final executable, namely find.

it's a good thing that we have something like this because that way we could simply avoid compiling everything every time we add a feature or fix a bug in one of the source code files. all we need to do now is just to compile the source code file where we added/modified code and link the object files together.

one way to fix the error is by having a command similar to the one above except that you will need to replace every occurrence of find with the name of your source code file (excluding the extension). for example, if the source code file of your testing program is called foo.c, you could have a command like

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

you could add a new target in the makefile that executes this command. you can do so by adding

foo: foo.c helpers.c helpers.h
    clang -ggdb3 -O0 -std=c99 -Wall -Werror -o foo foo.c helpers.c -lcs50 -lm

at the end of the file. make sure there is a tab before clang not spaces!

this way, executing

make foo

should do the trick. and ./foo should execute your program.


if you're curious, another way to fix that assuming you don't need to compile your testing program every time you make a modification to helpers.c only, you could compile your program with the -c option to produce an object file by executing the command below only once

clang -ggdb3 -O0 -std=c99 -Wall -Werror -c -o foo.o foo.c -lcs50 -lm

this produces an object file to your program called foo.o. now all you need to do is to compile helpers.c which could be done easily through executing

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

and finally all you need to do is to link the two together using

clang -o foo foo.o helpers.o

this will produce the executable foo which you can execute using ./foo.

every time from now on, you only need the last two commands to compile your program (again assuming you only modified helpers.c). this first of the two will compile helpers.c into helpers.o and the second one will link helpers.o with foo.o (which is compiled already) to produce the final executable foo.

6
  • Excellent explanation, Kareem. It gets my vote. ;-)
    – Cliff B
    Jun 25, 2015 at 20:50
  • Thanks for the very detailed explanation Kareem. I've marked this as 'answering' my question, but I have to admit it's quite a bit over my head at this point. Looking forward to compiler instructions being covered more later in the course. Jun 26, 2015 at 1:35
  • Kareem, I've actually already executed the code as you mention in the above post - just copied the format of running make find. It produces a long list of errors and ultimately says "undefined reference to main" and "clang: error: linker command failed with exit code 1." I can't spare any more time to mess with it, so I'm just going to copy and paste my sort function into my test file. Jun 26, 2015 at 1:41
  • @BrianPoindexter I'm sorry! it's not clear to me what you copied exactly. do you mean the makefile modification? if that's what you're referring to, did you ensure that there is a tab before clang and not spaces?
    – kzidane
    Jun 26, 2015 at 1:59
  • @Kareem, no I'm referring to the clang command. clang -ggdb3 -O0 -std=c99 -Wall -Werror -o foo foo.c helpers.c -lcs50 -lm Jun 26, 2015 at 12:17
0

The error is saying that it can't find the sort function. The reason is nothing to do with your code. You made a file called testsort.c to test this out. Unfortunately, there is nothing in the makefile that says how to actually make testsort, so it can't find the helpers files.

If you want to build the file, you need to make a new makefile or edit the current one and add the needed info on how to make testsort. There is much more discussion on this topic in a later lesson.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

You must log in to answer this question.

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