views:

473

answers:

5

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..

+1  A: 

It doesn't really matter as long as it's not likely to be used anywhere else. I usually go with something like BLAFOO_H_INCLUDED.

isbadawi
+13  A: 

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.

nos
+1 for mentioning that identifiers beginning with `_` are reserved for the implementation
Adam Rosenfield
+2  A: 

The only real requirement is that it won't conflict with another project that uses the same name for its file. For all of the projects I've seen, it usually completely quantifies the namespace (or whatever folder the file is in for C) along with the project name. Sometimes it includes the date the file was created too.

So if you're working on project ABC in folder DEF today, then you could do:

#ifndef ABC_DEF_BLAFOO_H_05_30_2010

And this is very unlikely to conflict with anything.

Jonathan Sternberg
+3  A: 

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

AOI Karasu
How does one generate a UUID? Do you use an automated tool or a function in your IDE?
Thomas Matthews
I also think using a GUID is a good idea, but a couple comments - strictly speaking you shouldn't use the `__` prefix as it's reserved for the implementation (though there's little chance for a conflict with the GUID in there). Also, there's a good chance that you'll find others on your team don't like it - at least that's what I've found. In that situation I don't push it since I can't recall a situation where an include guard conflict actually caused a problem.
Michael Burr
@Thomas - on Windows, Visual Studio (and/or the Windows SDK) provide a command line tool, `uuidgen`, and a GUI tool, `guidgen`. I'm sure there are similar tools for Unix systems.
Michael Burr
@Michael: At first I didn't like the idea myself and considered it unreadable / inconvenient to use. However I realized it's the only easiest way for me to have a totally unique identifier and decided to stick to these simple rules: 1. Never refer to it anywhere in the code, 2. Use the same UUID for all headers in particular lib/project.BTW - what do you mean by "it's reserved for the implementation"? Implementation of what? Reserved by whom? Thanks.
AOI Karasu
@AOI: I don't think there's much reason to bother trying to make UUID the same for all the headers within a project - UUIDs are free, just give each header its own then forget about it. As far as "reserved", see http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797 for details. Like I said, though - the chances of your macros conflicting with anything is pretty much impossible given the UUIDs in there, but there's no reason for the double underscores anyway, so I'd say drop 'em.
Michael Burr
+2  A: 

As with other C-style questions, just be consistent. There is no way that you are going to know the namespace every library that someone might link with your program in the future. Why? Many of them have not been written yet :)

As such, its not a question of include guards, its a question of what to name the header in the first place.

I might come up with some cool new string utilities, and name the header strutil. That's a bad idea, because (surely) someone else has come up with cool new string utilities and named the header the same.

So, I name mine post_strutils.h and:

#ifndef POST_STRUTILS_H
#define POST_STRUTILS_H
/* code */
#endif

I may even call it post_str_utils.h and define the include guards appropriately because I know that I have a very common last name. Finding a namespace is sometimes difficult. Simply using one offers no guarantee that someone else did a search prior to releasing something to the wild. Be as unique as possible.

Depending on where someone tells their compiler to search for headers, its not just namespace conflicts that come into play, its also file names. Do your best to name the header uniquely, then write the include guard to match it. Someone might want to #error if the header has been included multiple times, if only to cut #include directives that aren't needed, using a UUID kind of makes doing so confusing, since it doesn't match (or even resemble) the file name of the header in question. It also makes grep/awk(or similar) powered lint scripts harder to write.

I'm not saying you should name every library / module after yourself, but do take care to make the public header file names unique. A quick conference with a search engine will tell you if you hit on an unused namespace. Please, let the include guards match (or at least closely resemble) the header. Again, consistency is highly praised. From your example, I'd expect:

int blahfoo_init(void);
double blahfoo_getval(blahfoo_t *blah);

If you go through the bother of finding a unique namespace, be sure to use it :)

Tim Post
+1 for "namespace" prefix
Emile Cormier