Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have an ${include_guard_symbol} in Eclipse with an uppercase file path?

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?

like image 264
alestanis Avatar asked Oct 19 '12 13:10

alestanis


2 Answers

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

  1. Exit Eclipse
  2. Navigate to your workspace folder and then keep navigating to \.metadata\.plugins\org.eclipse.core.runtime\settings
  3. I always like to make a backup of the settings folder before making mods
  4. Load the file named org.eclipse.cdt.ui.prefs into a text editor
  5. Add this line (I put mine at line 3)
    codetemplates.includeGuardGenerationScheme=2
  6. Save the file.
  7. Restart eclipse

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_ */
like image 170
Tod Avatar answered Oct 23 '22 23:10

Tod


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.

like image 20
Software_Designer Avatar answered Oct 23 '22 22:10

Software_Designer