Skip to main content
1 of 3
Air
  • 2.1k
  • 18
  • 29

If you take a look at the distribution code for find.c, you'll find this line near the top of the file:

#include "helpers.h"

This #include statement is different from the others in that it uses quotes ("") rather than angle brackets (<>). This is because helpers.h is a header file written just for you, and it's located in the same directory as find.c -- unlike stdio.h, for example, which is located somewhere in a directory of standard library header files.

So, that's how 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 headers.h inside the #include directive instead of just the file name.

As for compiling the header files, that happens automatically when you compile find.c, thanks to the Makefile that's included in the same folder; it contains instructions for make, as explained in the pset specs:

Notice further that you just compiled a program comprising not one but two .c files: helpers.c and find.c. How did make know what to do? Well, again, open up Makefile to see the man behind the curtain. The relevant lines appear below.

find: find.c helpers.c helpers.h
    clang -ggdb -std=c99 -Wall -Werror -o find find.c helpers.c -lcs50 -lm

Per the dependencies implied above (after the colon), any changes to find.c, helpers.c, or helpers.h will compel make to rebuild find the next time it’s invoked for this target.

In other words, you don't have to compile helpers.c explicitly; make is issuing all the appropriate commands to the compiler when you execute make find, because that's how the distribution code was set up for you.

If you were to write some code from scratch using header files like this, you would also need to know something about clang or another compiler in order to issue the proper commands and/or write a Makefile to use with your project.

Air
  • 2.1k
  • 18
  • 29