If you take a lookLooking at the distribution code for find.c
, you'll findwe can see this line near the top of the file:
#include "helpers.h"
#include "helpers.h"
This #include
statement is different from the others in that it uses quotes (""
) rather than angle brackets (<>
). This is becauseThat header file, helpers.h
is a header file written just for you, and it's located incontains only the same directory asprototypes for your find.csearch
and -- unlikesort
functions; the actual implementations are in stdiohelpers.hc
, for example, which is located somewhere in a directory of standard library header files.
1 So, that's how do we get from find.c
knows where the header code is. If you moved the header file to another folder, you would need to specify the full path to headershelpers.hc
insidewithout the line #include helpers.c
directive instead of justsomewhere in the file name.mix?
As for compiling the header files,The answer is that it all happens automatically when you compilethanks to the find.cmake
utility -- and more specifically, thanks to the Makefile
that's included in the same folder; itfind
directory. The Makefile
contains instructions for make
, as explained in the pset specs:
How the Makefile
works is explained in a recommended reading from the beginning of the specs:
This makefile contains two types of lines. The lines appearing flush
left are dependency lines. The lines preceded by a tab are
executable lines, which can contain any valid UNIX command. A
dependency line says that some file is dependent on some other set of
files. For example, main.o: main.c util.h
says that the file
main.o
is dependent on the files main.c
and util.h
. If either of
these two files changes, the following executable line(s) should be
executed to recreate main.o
.
In other wordsour case, you don't havewhat links the internal implementation of the header file prototypes to find.c
at compile time is that:
- The dependency line of our
Makefile
shows that find
depends on helpers.c
;
- The executable line that follows provides that same dependency as a command-line argument to the compiler,
clang
.
Why isn't there a helpers.h
argument, if it's listed as a dependency? Because it's already there -- we included it in find.c
explicitly;-- we just need to remind make
is issuing allthat it's there, because make
looks for changes in the appropriate commands todependencies before clang
is executed and the compiler when#include
directive is processed.
So why don't you executehave to make#include findhelpers.c
, because that's how? The straight answer is that all you are required to provide in the distribution code was set upsource file, at a minimum, is prototypes for the functions you call. Notice you didn't #include <stdio.c>
either, just a header (.h
) file!
If you were to write some code from scratch using header files like thisAs far as find.c
is concerned, the only functions you would also need are search
and sort
, which are prototyped in helpers.h
. You don't really need to know something aboutthe details of how clangsearch
or another compileris implemented in order to issue the proper commandscall it -- in fact, you could modify helpers.c
all you wanted and/or write a as long as the prototypes in helpers.h
didn't change, you wouldn't have to make any changes to find.c
.
Is this the only way to do it? No; you could put all of the implementation from headers.c
into headers.h
instead, remove references to headers.c
from the Makefile
, and it would still work. But the convention when writing C code is to usedo it this way; if you're interested in the reasons, there are some discussions on other Stack Exchange sites that you could look up -- I'll start you off with your project.this one: Why do we need to write a header file?