Normally, your functions.h
would contain:
extern int sum(int x, int y);
possibly wrapped with header guards, and possibly omitting extern
*. You should include the header in the file that defines the function, and also in any file that uses it. Both parts are crucial; they allow the header to ensure that the uses of the function agree with the definition of the function.
C99 requires functions to be declared or defined before they are used, rather like C++.
FWIW: I usually compile with GCC and usually use the options:
gcc -g -O3 -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
-Wold-style-definition -Wold-style-declaration -Werror \
...other arguments as appropriate...
The -Werror
means my code compiles without warnings! You'll also see me add static
to functions defined in a single-file program to avoid warnings (aka errors) though I don't always bother to comment on why I make that change.
* Normally, headers should include header guards. For a header that neither needs any other headers nor defines any types, it is almost permissible to omit the header guards (I'd put them in anyway, but as long as you're cognizant of the issues, omitting them would be OK too). I prefer to use extern
even on functions, though it is not necessary.
For variables, it is crucial.