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
.