24

Is there a way to customize the format of inclusion guards in eclipse CDT for the class generation template? The current format is <Class Name>_H, but what I would like is something like <namespace>_<namespace>_<class name>_H. Not that I expect to have classes with the same name in different namespaces within my own project, but I'd prefer not to worry about it should the case arise.

4
  • I recently asked something similiar (no answers yet): stackoverflow.com/questions/3520943/…
    – IanH
    Aug 23, 2010 at 13:37
  • I also asked the same question before: stackoverflow.com/questions/5402665/… There are some answers, but nothing really satisfactory.
    – LiKao
    Mar 8, 2012 at 19:55
  • 1
    I realize this may not be exactly what you're looking for, but for any compiler that isn't really ancient or really weird, I prefer #pragma once.
    – Reinderien
    Aug 13, 2012 at 17:45
  • 2
    The trouble with #pragma once is that as it's not a standard part of the language you can't be sure that any compiler (including sexy modern ones) will recognize it. Include guards are still the safest, most portable, solution.
    – dajames
    Oct 10, 2012 at 13:04

3 Answers 3

2

There is hard way to do this. You can rebuild plugin CDT plugin. Information about code repository and needed enviroment for rebuild available here. In your case you need change behavior of generateIncludeGuardSymbol() method that in

core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java

More about it described in this answer

2

I like Stuart's answer here:

How to customize eclipse CDT code templates

Just customize the template.

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} */
1

This is in Neon...

Open up Window/Preferences.
Go down to C/C++/Code Style/Name Style.
Under Code you will find Include Guard.
It looks like some customization is allowed there.

This is the thing that gets expanded to ${include_guard_symbol}.

2
  • 1
    Does not work for me. There is a radio button which offers 1) Filename path relative... 2) File name 3) Unique identifier. No editable string field. Eclipse Neon 4.6.0.
    – Twonky
    Feb 15, 2017 at 7:50
  • Does not work for me either. Has anyone found out how to edit that?? I need to remove trailing underscore that I have.
    – bone
    Apr 13, 2017 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.