I have three files.
trees.h
// a bunch of typedefs and function prototypes
#include <trees.c>
trees.c
// a bunch of function bodies
main.c
#include <trees.h>
This isn’t right, because every function in trees.c is giving me “undefined type” errors about the type defined in trees.h. I’ve tried every configuration I can think of – include trees.c from main and include trees.h from trees.c, include trees.c at the end of trees.h, include it at the beginning of trees.h… Every combination of includes I could think of, and each one gives a different set of errors. Sometimes it’s multiple defines, sometimes it’s undefined functions…
So how exactly does this work? What do I put in which files, and which files do I include and where?
Like this:
Explanation:
#includeis basically the same as copying the entire included file to this file (where you put the#include).So including
trees.hinmain.callows that file to know about functions intrees.c.Including
trees.hintrees.callows functions lower down intrees.cto be usable and this is also where defines, etc. used intrees.cis specified.You also may not know about creating multiple objects and linking them, refer to Joachim Pileborg’s answer.
The (very ugly) alternative is:
Then you just need to compile
main. But for any project of a few.cfiles, this becomes impractical.