Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize eclipse CDT code templates

I need the code I am writing for a project to match some style guidelines. However the standard templates included with CDT don't match this style. Especially the layout of the header guards is not the way it should be. I had a look at the template and for my Eclipse it looks like this:

${filecomment}

#ifndef ${include_guard_symbol}
#define ${include_guard_symbol}

${typecomment}
${declarations}

#endif /* ${include_guard_symbol} */

So I am guessing the variable ${include_guard_symbol} is set somewhere in the CDT, but is it possible to change this setting without needing to modify the CDT itself?

On a slightly different, but related note: Is it possible to add your own templates, so you just could add new files of other types (test-cases, specialized classes etc) using the normal new dialog for the project?

like image 283
LiKao Avatar asked Mar 23 '11 08:03

LiKao


People also ask

What is Eclipse CDT builder?

A CDT project typically has two builders. The first one is the CDT builder which is responsible for compiling your code. If you are using an external build tool you are most likely using a "makefile project", in which case the CDT builder simply invokes your build tool for you.


1 Answers

We've had a similar struggle on our project. 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 121
Ogre Psalm33 Avatar answered Oct 29 '22 15:10

Ogre Psalm33