Custom C/C++ Headers with Eclipse

One of the great things in Eclipse is that it can be customized down to the bits. The downside (pun intended) is that it might not be obvious how to do this. So here is how you can create your own custom file header when creating a new C/C++ file in Eclipse:

Eclipse uses the concept of Code Templates which can be configured with lots of things. One template is for comments which is used for new files. You can find this under Preferences > C/C++ > Code Style > Code Templates:

Default Code Template

This would create a comment in the header like this:

default header file comment

Well, that’s maybe not something I want.

So I can easily change this to something like this or whatever I like:

/*
 * Copyright (c) ${year}, Erich Styger
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
Custom file comment

It is possible to use several built-in variables like ${user} or ${year} which I have used above.

This gives me a standard header like the one below I can use for my sources:

Modified default comment header

If you would like to change or tweak all the other things in a new file, for example a header file, Eclipse has you covered too:

Default header template

Be free to tweak and change things as you like. Just remember that these settings are workspace settings, but you can share them in a team so everyone is using the same template. That way you can conform to your company rules and guidelines automatically without editing the file after you have created it.

Happy Headering 🙂

Links

6 thoughts on “Custom C/C++ Headers with Eclipse

  1. Hey Erich,

    Always good – What I would really like is a way that headers are updated for copied in source files. we have headers in our current projects that go back to 2009 and they’re a pain to update (so nobody does that as there’s always danger that something will get accidentally changed).

    myke

    Liked by 1 person

    • Hey Myke,
      it really depends what you want and need to do. There is a refactoring feature in Eclipse, plus a ‘global search and replace’ for example in a project. So if you have a text/string to update, this could be done that way. To my knowledge there is currently no customizable action for ‘copy file’ operation. If it is about updating the copyright year, this only would be touched if there are ‘substantial’ changes warranting a year update, so this would be a manual process anyway.

      Liked by 1 person

      • Hi Erich,

        Know about those but you have to put in manually what you’re looking for and if you’re going through all that trouble, you might as well manually do it yourself.

        Again, thanx for a great article.

        Liked by 1 person

  2. Pingback: Spilling the Beans: C/C++ Header Files | MCU on Eclipse

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.