what is concerned best practice regarding the following "pattern"?
#ifndef BLAFOO_H
#define BLAFOO_H
/* ...
* ...
*/
#endif /* BLAFOO_H */
how should i name the header in the #define
directive? i've seen all from said BLAFOO_H
to __BLAFOO_H
to _BLAFOO_H_
etc..
The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .
Generally it's best to have a header file for each . c file, containing the declarations for functions etc in the . c file that you want to expose.
The GNU C Library header files have been written in such a way that it doesn't matter if a header file is accidentally included more than once; including a header file a second time has no effect. Likewise, if your program needs to include multiple header files, the order in which they are included doesn't matter.
Header files should #include the minimum header files necessary, and source files should also, though it's not as important for source files. The source file will have the headers it #include s, and the headers they #include , and so on up to the maximum nesting depth.
Name them BLAFOO_H
(personnally I use BLAFOO_H_
where BLAFOO is the header file name ).
Make sure your BLAFOO doesn't clash with other files/libraries/etc. you're using, e.g. have your project and/or module name be parth of that name.
Identifiers starting with a _
is reserved for the implementation/compiler, so don't use that.
I use an UUID that is my guarantee that #define
not clashed with others. I've seen it somewhere and decided to use it as well.
My pattern is like this: __<filename>_H_<uuid>__
,
eg. #define __TYPES_H_79057761_16D6_478A_BFBC_BBF17BD3E9B9__
for a file named types.h
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With