Re: About header files



Christoph Scholtes said:

Hi,

I have some questions about header files:

Say I have a file functions.c which contains a couple of functions. I
have declared some structs in this file too. The structs are defined in
main.c. Now I create a header file which represents the interface of
functions.c to my main program file main.c.
I put in the header file: all function prototypes with keyword extern
and the declarations of the structs, which are defined in the main
program.

Do I include the header file functions.h in my file functions.c to get
the struct declarations or do I explicitely declare the structs in
functions.c and not include the header file?

Unless you are doing opaque types:

struct type definition goes in header
struct definitions go in the functions that need them

If you are doing opaque types:
struct type definition goes in module's .c file
struct type declaration goes in module's header
pointers to struct types are defined in the functions that need them



About standard libraries: functions.c uses stdio.h, stdlib.h and
string.h.

Those are headers, not libraries.

Thus, I include the header files. main.c also uses stdlib.c
and stdio.h. Do I include them again or do I use inclusion guards to
check if they are already included (e.g. in functions.c)?

Include them as often as you like. They are already protected by inclusion
guards.

If a variable is declared static, is it still legal to pass it on as a
function argument to a function outside of the file?

Yes.

The keyword extern in front of a function prototype in my header file
says that the function is defined somewhere else, right?

I certainly hope so, since you wouldn't want to define the function in the
header file!!

Do I HAVE to
declare the function prototype as extern in the header file?

No. For functions, it's the default, and it can be omitted. Indeed, it
almost always /is/ omitted.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.



Relevant Pages

  • Re: [PATCH] proc_fs.h redux
    ... struct file_operations; ... If you go down that route, you end up with _lots_ of circular ... dependencies - header file X needs Y needs Z which needs X. We've ... The only realistic solution is to use forward declarations. ...
    (Linux-Kernel)
  • [PATCH] remove unnecessary #includes from <linux/fs.h>
    ... >> everything being rebuilt just because one header file was touched. ... struct nameidata; ... extern int is_subdir ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Newbie question
    ... >>> What I've done is declare a struct in a header file, ... >> Declaring structs, typedefs, function prototypes etc yes, defining ... >the header file and not again referenced in the code. ... >the header file in each code file is a duplicate declaration, ...
    (comp.lang.c)
  • [PATCH 3/16] 2.6.17-rc6 perfmon2 patch for review: new generic header files
    ... +#ifdef CONFIG_PERFMON ... * PMD/PMC return flags in case of error ... +struct pfarg_ctx { ... * This part of the header file is meant for kernel level code only including ...
    (Linux-Kernel)
  • [PATCH 3/17] 2.6.17.1 perfmon2 patch for review: new generic header files
    ... +#ifdef CONFIG_PERFMON ... * PMD/PMC return flags in case of error ... +struct pfarg_ctx { ... * This part of the header file is meant for kernel level code only including ...
    (Linux-Kernel)