-2

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?

2
  • 1
    Why are you including tree.c in anything?
    – Mat
    Feb 13, 2013 at 11:05
  • The preprocessing directive #include basically copy the content of the file you include at the location you specified this include (see msdn.microsoft.com/en-us/library/36k2cdd4%28v=vs.71%29.aspx). Thus, your #include <trees.c> is the reason you have this undefined type errors : you have copied the implementation of the function before their prototypes.
    – JBL
    Feb 13, 2013 at 11:13

5 Answers 5

1

Like this:

trees.h
// a bunch of typedefs and function declarations (prototypes)

trees.c
#include <trees.h>
// a bunch of function definitions (bodies)

main.c
#include <trees.h>

Explanation:

#include is basically the same as copying the entire included file to this file (where you put the #include).

So including trees.h in main.c allows that file to know about functions in trees.c.

Including trees.h in trees.c allows functions lower down in trees.c to be usable and this is also where defines, etc. used in trees.c is specified.

You also may not know about creating multiple objects and linking them, refer to Joachim Pileborg's answer.

The (very ugly) alternative is:

trees.h
// a bunch of typedefs and function declarations (prototypes)

trees.c
#include <trees.h>
// a bunch of function definitions (bodies)

main.c
#include <trees.c>

Then you just need to compile main. But for any project of a few .c files, this becomes impractical.

2
  • But in the first example, how does main.c know what the definitions of the functions in trees.h are? Why would the compiler not just throw some sort of "function prototype without definition" error? How is it that it knows to look in trees.c - which is mentioned nowhere - to find those definitions?
    – Jack M
    Feb 13, 2013 at 12:24
  • 1
    @JackM: in the first example, the compiler does not look in trees.c while compiling main.c. All that's needed to compile main.c is the prototypes, not the function bodies. The linker will later need the function bodies, which is why you need to know about creating multiple object files and linking them. Feb 13, 2013 at 12:28
1

You compile each source file into object files, then link these object files together to form the final executable. If you use e.g. GCC and compile on the command line you can put both source files in the command line and GCC will handle it for you:

$ gcc main.c tree.c -o tree

The above command tells gcc to compile and link the files main.c and tree.c, and name the output executable tree. You can also compile the source file separately into object files, and then manually link them together:

$ gcc -c main.c -o main.o
$ gcc -c tree.c -o tree.o
$ gcc main.o tree.o -o tree

In the above commands, the -c option tells gcc to create an object file, and the -o option tells gcc what to name that object file. The last command takes these object files and create the final executable and names it (with the -o option) tree.

In the source files, you include the header(s) needed, not the other way around.

1

The reason it's not working is because you have to use 'extern' to define a variable or function that is declared in a different include file to the standard ones. You should also save all header files as *.h. So, assuming you had defined the struct 'tree' in tree.h:

#include <tree.h>

int main() {
    extern tree; /* Get variable 'tree' declared in tree.h */
    extern make_tree(void); /* Get function make_tree() declared in tree.h */

    make_tree(); /* Call function */
    return (0); /* End */
}
0

Begin with not including implementation (.c) files. Only include header (.h) files. Include them whenever you need the types or functions declared in them. To avoid multiple declarations, use include guards (whatever they are - just google it).

0

The way to go is including your header file trees.h in trees.c and then including trees.c within main.c

Have a look at this thread here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.