9

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?

Community
  • 1
  • 1
alestanis
  • 21,519
  • 4
  • 48
  • 67

3 Answers3

4

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_ */
Community
  • 1
  • 1
Tod
  • 8,192
  • 5
  • 52
  • 93
1

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.

Community
  • 1
  • 1
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
  • Thank you for your answer. I had already seen the answer you linked, but as I said in my question, it's not what I'm looking for. I would like to have the folder inside the include guard, not just a project name. – alestanis Nov 07 '12 at 13:40
0

Not really an answer to your question, but I would like to suggest an alternative. Include guards provide a working, albeit crude way to forbid code in a header file to be included more than once per compilation unit. As an alternative, you might use the

#pragma once

compiler directive. I realise that it is not defined in the standard, but it is supported by numerous compilers, including GNU, Clang, MSVC, and Intel. If you use #pragma once, you loose little portability and you avoid name clashes which I assume is the reason you want to change ${include_guard_symbol} in the first place.

You might also check out http://en.wikipedia.org/wiki/Pragma_once for a more thorough discussion.

Michael Koenig
  • 364
  • 2
  • 9
  • Thanks, but that's not an option, I work on multi platform / compiler projects, and don't want to go that way. http://stackoverflow.com/a/1696194/1225541 – alestanis Nov 14 '12 at 12:35