When defining code templates in Eclipse CDT, we can use a variable named ${include_guard_symbol}
that translates to MYFILE_H
.
I would like to have something more explicit: SRC_MYFOLDER_MYFILE_H
.
I followed the steps given in an answer to Stack Overflow question Customizing include-guard for Eclipse CDT, but all I get is for ${include_guard_symbol}
to return an empty string! I also saw other related questions on Stack Overflow about adding the namespace to the include guard, but that's not what I'm looking for.
I am using Eclipse version 3.5.2 with CDT version 6.0.2.
Is there another way of achieving the desired result?
The oldest version I have installed is 3.7 and I tested there and on 4.2 and the referenced link does exactly what the OP wants. (The OP is using 3.5.2). For anyone coming here in the future here are the steps
\.metadata\.plugins\org.eclipse.core.runtime\settings
org.eclipse.cdt.ui.prefs
into a text editorcodetemplates.includeGuardGenerationScheme=2
I created a folder named MyFolder under my src folder. Then I right-clicked and added a new header file the result was:
#ifndef SRC_MYFOLDER_TEST_H_
#define SRC_MYFOLDER_TEST_H_
#endif /* SRC_MYFOLDER_TEST_H_ */
Main points from this: How to customize eclipse CDT code templates
One solution is to throw out ${include_guard_symbol}
in the template all together, and define it yourself, possibly using some of the other predefined variables. Something like this:
${filecomment}
#ifndef MyProject_${file_base}_h
#define MyProject_${file_base}_h
${typecomment}
${declarations}
#endif /* MyProject_${file_base}_h */
So for a header file named inc/Foo.h, the include guard would be inserted like this:
#ifndef MyProject_Foo_h
#define MyProject_Foo_h
Unfortunately, there doesn't seem to be a way to customize much beyond that. For example, if I defined a class nested in a namespace, I might want to put the namespace as part of the include guard. I can't find a way to do that in eclipse, currently.
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