3

By default eclipse creates include/header guards as follows:

For instance for test.h

#ifndef TEST_H_
#define TEST_H_
...

I am wondering if this convention can create collisions when we have files with same names under different folders(namespaces) within the same project. If I am not overlooking something it will be a problem when we have a situation like that. I am also wondering what the best practice is when naming include guards. I've seen some code examples where guid based naming is used, I am wondering if eclipse supports that out of the box, if not can anybody suggest a plugin for that.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
erin c
  • 1,345
  • 2
  • 20
  • 36

3 Answers3

0

If you use a reasonably modern compiler, you can replace these guards with more elegant directive #pragma once.

Look at things like Boost files, they have some conventions for the header guards.

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
  • 1
    since it is non-standard feature, that will make code more compiler-dependent, less portable. Therefore I can't use #pragma once. – erin c Jan 04 '12 at 13:20
0

If the file name can appear in several folders or namespaces, you can of course include the namespace as well in the include guards.

The combination of namespace and class name will have to be unique enough in your project anyway.

In MyNamespace/MyClass.h

#if !defined MYNAMESPACE_MYCLASS_H_INCLUDED
#define MYNAMESPACE_MYCLASS_H_INCLUDED
...
#endif
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • of course I can add namespace manually, my question is about how to auto include namespace or guid into include guards, so I don't have to do it manually every time I create a class. – erin c Jan 04 '12 at 23:35
-1

Name should be unique across all the header files. XXXX_H_ is common, as is _XXXX_H. GUIDs are rarely used and least prone to collosion.

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
  • 1
    so what is the solution that you're proposing since files can have same names under different namespaces? I went code templates path, it looks promising to me especially if I can find a way to generate a guid from a code template script. – erin c Jan 04 '12 at 12:19
  • I saw the link you posted, it doesn't answer my question. In my case file names aren't necessarily unique, therefore there can be header guard name collisions. I don't want to use pragma once, I want a way to make header guard name unique. – erin c Jan 04 '12 at 12:24