15

Is it possible (and how) to add the namespace in the name of the automatic generated include guards in Eclipse CDT, when creating a new class using the .hpp/.cpp templates?

For me Eclipse generates a new class with a namespace nicely, but the include guards do not contain the namespace, so if the same header file exists twice in two different directories, only one can be included.

In my case the name of the namespace, the Eclipse project name and the name of the source directory are all the same, so these could be alternatives as prefix for the include guard.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
IanH
  • 3,968
  • 2
  • 23
  • 26
  • Good question - Using Eclipse Indigo + CDT, I looked under the Prefs -> C++ -> Code Style -> Code Templates -> Files -> C++ Header File -> Default C++ Header Template and there's no variable for namespace. So currently I think there's no answer to this (still) – synthesizerpatel Jul 20 '11 at 18:06

2 Answers2

6

So in the Preferences dialog under C/C++ -> Code Style -> Code Templates you can modify the template to be closer to what you need, for example if you need the namespace in the guard, you can do something like.

${filecomment}

#ifndef ${namespace_name}_${include_guard_symbol}
#define ${namespace_name}_${include_guard_symbol}

${includes}

${namespace_begin}

${declarations}

${namespace_end}

#endif /* ${namespace_name}_${include_guard_symbol} */`
Stuart
  • 330
  • 4
  • 10
  • I did this, but how do you specify the namespace so the namespace elements are all filled in properly? If I use the New -> Header File, there is no namespace entry field. – Roderick Taylor Aug 03 '11 at 05:11
  • Yes indeed, if you create a new class it will create the header and cpp file for you all filled out (if you enter a namespace of course) – Stuart Oct 19 '11 at 17:39
2

There's a hidden preference you can set to get at least the file's path or a uuid in there instead of just CLASSNAME_H_. See my full answer here.

Community
  • 1
  • 1
Dave E
  • 612
  • 6
  • 13